Hi, I''ve got 3 models: Comment, Venue and User. Comment has many :venues and belongs to :user. Venue belongs to :user as well. When adding a comment, user should be able to attach a venue to it. In Venue model "user_id" attribute is protected and it is required as well - this causes problem when trying to create a venue when adding a comment using nested attributes, because I can''t pass "user_id" attribute required by venue - it''s protected and thus rejected. Any ideas how to solve it? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi Szimek,
You can do it like this:
# hash to simulate what comes from your params[:comment]
comment = { :text => "Comment text", :venues_attributes => [{
:name => "one"
}, { :name => "two" }] }
# you set the user id by calling a block on create
Comment.create(model) { |c| c.venues.each { |v| v.user_id = 1 } }
Hope this helps.
/Lasse
2010/3/26 szimek <szimek-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> Hi,
>
> I''ve got 3 models: Comment, Venue and User. Comment has many
:venues
> and belongs to :user. Venue belongs to :user as well.
>
> When adding a comment, user should be able to attach a venue to it.
>
> In Venue model "user_id" attribute is protected and it is
required as
> well - this causes problem when trying to create a venue when adding a
> comment using nested attributes, because I can''t pass
"user_id"
> attribute required by venue - it''s protected and thus rejected.
>
> Any ideas how to solve it?
>
> --
> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To unsubscribe from this group, send email to
>
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
> .
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>
--
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Whoops, Comment.create(model) should of course be Comment.create(comment) And you should be able to replace it with Comment.create(params[:comment]) /Lasse 2010/3/27 Lasse Bunk <lassebunk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> Hi Szimek, > > You can do it like this: > > # hash to simulate what comes from your params[:comment] > comment = { :text => "Comment text", :venues_attributes => [{ :name => > "one" }, { :name => "two" }] } > > # you set the user id by calling a block on create > Comment.create(model) { |c| c.venues.each { |v| v.user_id = 1 } } > > Hope this helps. > > /Lasse > > 2010/3/26 szimek <szimek-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > Hi, >> >> I''ve got 3 models: Comment, Venue and User. Comment has many :venues >> and belongs to :user. Venue belongs to :user as well. >> >> When adding a comment, user should be able to attach a venue to it. >> >> In Venue model "user_id" attribute is protected and it is required as >> well - this causes problem when trying to create a venue when adding a >> comment using nested attributes, because I can''t pass "user_id" >> attribute required by venue - it''s protected and thus rejected. >> >> Any ideas how to solve it? >> >> -- >> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> >> . >> For more options, visit this group at >> http://groups.google.com/group/rubyonrails-talk?hl=en. >> >> >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.