rubyonrailsin

A Ruby and Rails talk

Saturday, March 27, 2010


Re: [Rails] Nested attributes with required protected attributes

by rubyonrailsin 0 comments

Tag


Share this post:
Design Float
StumbleUpon
Reddit

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@gmail.com>
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@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.


--
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

Subscribe feeds via e-mail

Blog Archive