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!

share|improve this question

1 Answer

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

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.