1

I have three models

class Property < ActiveRecord::Base
  has_many :contact
  accepts_nested_attributes_for :contact
  has_many :business
  accepts_nested_attributes_for :business
end

class Business < ActiveRecord::Base
  belongs_to :property
  has_many :contact
end

class Contact < ActiveRecord::Base
  belongs_to :property
  belongs_to :business
end

I created a form that creates the Property with a nested contact and a nested business, how can I get that business to have a nested contact?

Here is my form

 <%= form_for(@property) do |f| %>
  <div class="field">
    <%= f.label :address %><br>
    <%= f.text_field :address %>
  </div>
 <% end %>
 <%= f.fields_for :contact do |contact_form| %>
  <div class="field">
    <%= f.label :contact_title, "Title" %><br>
    <%= contact_form.text_field :title %><br>

    <%= f.label :contact_name, "Name" %><br>
    <%= contact_form.text_field :name %><br>
  </div>
 <% end %>
 <%= f.fields_for :business do | business_form| %>
    <div class="indv-biz field">
      <%= f.label :business_name, "Name" %><br>
      <%= business_form.text_field :name %><br>
    </div>
     <div class="business-contact">
     <p>Business Contact</p>
  <%= f.fields_for :business_contact do | business_contact | %>
    <div class="field">
      <%= business_contact.label :contact_title, "Title" %><br>
      <%= business_contact.text_field :title %><br>
 <% end %>
 <% end %>

I can get it to save so the business is connected to the property and the contact is connected to the property but I can't figure out how to get a contact connected to the business

Thanks

1 Answer 1

1

You should try deep nesting like this. your requirement is a property have many business which in-turn have many contacts. In this case, you actually should do is setting nested form for property with business and that business should have nested form of contacts. The below one will work for you.

Form

nested_form_for @property do |f| 
  ...
  f.fields_for :bussiness do |bussiness_form|
    ...
    bussiness_form.fields_for :contact_form do |contact_form|
       ....
      end
    end
  end
end

Models

class Property < ActiveRecord::Base
  has_many :contact

  has_many :business
  accepts_nested_attributes_for :business
end

class Business < ActiveRecord::Base
  belongs_to :property
  has_many :contacts
  accepts_nested_attributes_for :contacts
end

class Contact < ActiveRecord::Base
  belongs_to :property
  belongs_to :business
end

controller

def property_params
      params.require(:property).permit(:id,.., :bussiness_attributes => [:id,.., , :contacts_attributes => [:id, ..]])
  end  
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, dude. I had already forgotten doing more than 2 models nesting was possible (I had done before).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.