Ruby on Rails Saturday, March 31, 2012

On Sat, Mar 31, 2012 at 7:59 PM, Linus Pettersson
<linus.pettersson@gmail.com> wrote:
> 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.

The correct wording is (somewhat confusingly)

@application.build_contact_person

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

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

HTH,

Peter

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment