How to set up these associations and nested form

Hi!

I’m creating a simple signup form where people can sign up for a camp. They should be able to add a contact person and also sign up other people (such as family members).

Two models:

Application

has_many :participants

accepts_nested_attributes_for :participants

Participant

belongs_to :application

I have some trouble with the contact person, which is also a participant. I guess I should add something like this:

Application

has_many :participants

belongs_to :contact_person, :class_name => “Participant”, :foreign_key => “contact_person_id”

accepts_nested_attributes_for :participants, :contact_person

Ok, so then I try to set up my nested form. In my ApplicationsController#new method I do this:

def new

@application = Application.new

2.times { @application.participants.build }

1.times { @application.contact_person.build }

end

But that gives me an error: “undefined method `build’ for nil:NilClass”. The error comes from: “@application.contact_person.build”, so contact_person is nil.

So, any ideas what I have done wrong? It was a while ago I fiddled with nested forms like this…

Thanks!

The correct wording is (somewhat confusingly)

@application.build_contact_person

Check these tables for the exact names of the singular and plural associations:

HTH,

Peter