Rails 5 error message: Child-Model Parent-Model must exist
Asked Answered
C

2

5

I have two models, Parent is Property, child is Phone. When attempting to create a new Property record with nested Phone data, I receive an error message: Phones property must exist.

I've studied the Rails Guide and a number of other documents without determining the cause. Here is a public github link if you want to see all the code: https://github.com/allenroulston/testnest.git

class Property < ApplicationRecord
  has_many :phones
  accepts_nested_attributes_for :phones
end

class Phone < ApplicationRecord
  belongs_to :property
end

# the form accepting the data
<%= form_for(property) do |f| %>
  <% if property.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(property.errors.count, "error") %> prohibited this property from being saved:</h2>

      <ul>
      <% property.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %>
    <%= f.text_field :name %>
  </div>

  <div class="field">
    <%= f.label :address %>
    <%= f.text_field :address %>
  </div>

  <div class="field">
    <%= f.label :city %>
    <%= f.text_field :city %>
  </div>

  <div class="field">
    <%= f.label "Telephone (example: 613 555 1234 )" %>
    <%= f.fields_for :phones do |p| %>
      Area Code <%= p.text_field :area %>
      Exchange <%= p.text_field :exchange %>
      Number <%= p.text_field :number %>
    <% end %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>


 # relevant controller methods ##################

 # GET /properties/new
  def new
    @property = Property.new
    @property.phones.build
  end

  # POST /properties
  # POST /properties.json
  def create
    @property = Property.new(property_params)

    respond_to do |format|
      if @property.save
        format.html { redirect_to @property, notice: 'Property was successfully created.' }
        format.json { render :show, status: :created, location: @property }
      else
        format.html { render :new }
        format.json { render json: @property.errors, status: :unprocessable_entity }
      end
    end
  end
Crisper answered 23/8, 2016 at 0:27 Comment(2)
Could you add the full stacktrace of your error?Bean
the error is displayed on the form, similar to a validation error. it is not an application error.Crisper
S
27

As far as I know, this is because when you use nested_attributes_for when creating a new object, the parent object is not yet created, so when attempting to create a reference to the parent object, the validation fails. To fix this you should change to: has_many :phones, inverse_of: :property.

Sill answered 23/8, 2016 at 10:38 Comment(5)
has_many :phones, inverse_of: :property does solve the "validation" error, although I admit I need to do some reading to understand why/how it solved the problem. Thank you.Crisper
@AllenRoulston great read for this is - viget.com/articles/… And you're welcome :) Would appreciate if you checked the question as answered :)Sill
I appreciate the article link. how do I mark this answered?Crisper
Press on the check mark on the left top corner of this answerSill
I faced same problem. After much head banging I realized that which model name will come first as per dictionary order is very important. For example "bar" has_many "foos" foo belongs_to "bar" since bar comes first in dictionary order you don't need inverse_of but you need it if "foo" has_many "bars" bar belongs_to foo in the second case inverse_of is required I think active record tires saving records in alphabetical order. In second case bar is saved before foo exists. So this error comes.Unwell
S
0

You need to add in the Property model as :-

accepts_nested_attributes_for :phones, reject_if: :all_blank, allow_destroy: true
Sarette answered 23/8, 2016 at 12:35 Comment(1)
Not really - Maybe he does not want to allow destruction through nested attributes and/or save empty attributes. This does not have anything to do with validations but more with what can come through the nested attributesSill

© 2022 - 2024 — McMap. All rights reserved.