Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm currently using the nested_forms gem and I'm trying to be able to add multiple landlords to a property.

At the moment the associations are quite deep: Property -> Landlord -> Contact_Detail -> Address

In my Property controller I'm building the associations and the initial form is displayed correctly. However, after using the add fields button, there are no fields. I know it is something to do with the object not getting built, but I can't understand why.

Here's my Property model:

belongs_to :address
belongs_to :estate_agent
belongs_to :property_style

has_and_belongs_to_many :landlord
has_and_belongs_to_many :tenancy_agreement

attr_accessible :landlord_attributes, :address_attributes, :estate_agent_attributes, 
:property_style_attributes, :sector, :reference , :occupied, :available_date, :property_style_attributes,...

accepts_nested_attributes_for :landlord, :address, :estate_agent, :property_style, :tenancy_agreement

And here's the new function in the Property controller:

  def new
    @property = Property.new
    @property.build_address
    @property.landlord.build.build_contact_detail.build_address

    @property.estate_agent_id = current_user.estate_agent_id

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @property }
    end
end

I've had quite a few attempts at this, but can't see where I'm going wrong, is it a problem with the nested_form gem not supporting this many levels of association or the type of association?

Thanks!

EDIT Changes made:

belongs_to :address
belongs_to :estate_agent
belongs_to :property_style

has_and_belongs_to_many :landlords
has_and_belongs_to_many :tenancy_agreements

attr_accessible :landlords_attributes, :address_attributes, :estate_agent_attributes, 
:property_style_attributes, :sector, :reference , :occupied, :available_date,  :property_style_attributes,...

accepts_nested_attributes_for :landlords, :address, :estate_agent, :property_style, :tenancy_agreements

Properties controller:

@property.landlords.build.build_contact_detail.build_address

Landlords model

has_and_belongs_to_many :properties

Here is my view:

<%= nested_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 |msg| %>
    <li><%= msg %></li>
  <% end %>
  </ul>
</div>
<% end %>

<h2>Landlords</h2>

<%= f.fields_for :landlords %>

<p><%= f.link_to_add "Add a Landlord", :landlords %></p>

 <div class="actions">
<%= f.submit %>
</div>
<% end %> 
share|improve this question

2 Answers

Unless you've specified "landlord" as an irregular inflection, Rails will assume that it is singular. Many-to-many associations should be declared in the plural.

Try changing the many-to-many associations to:

has_and_belongs_to_many :landlords
has_and_belongs_to_many :tenancy_agreements

You'll also need to change all calls to these to be plural as well. In addition, you must change the accepts_nested_attributes_for to landlords, and the attr_accessible from landlord_attributes to landlords_attributes.

share|improve this answer
Oh, I was under the impression that was the standard naming convention which had to used. I've changed it to plural everywhere, even in view and controller (was causing errors) but it still works as previously. I've noticed in my landlord model it has has_and_belongs_to_many :property will I need to change these to plural too? Thanks! – seekdestroy Apr 17 at 7:37
I just tried updating the landlord model to :properties, but still the same issue. Is it a problem with using the habtm association? – seekdestroy Apr 17 at 7:58

I attempted to use both awesome-nested-forms and cocoon and it still wouldn't work.

In the end, I found a workaround by building the object in the partial and not in the controller. Like this:

<% f.object.build_contact_detail.build_address %>

I hope this helps someone else!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.