Ruby on Rails
Thursday, February 28, 2013
On Tuesday, 26 February 2013 14:35:23 UTC-5, xop wrote:
When using "attributes=" for an object with a accepts_nested_attributes_for a modelthat has a has_many relationship will cause the has_many array to be appended-toinstead of being replaced as would be expected by a straight assignment operation.See this example:class Member < ActiveRecord::Basehas_many :postsaccepts_nested_attributes_for :postsendclass Post < ActiveRecord::Basebelongs_to :memberendm=Member.newp={:name => 'joe', :posts_attributes => [{:title=>'t1'},{:title=>'t2'}]} m.attributes = pm.posts.size ----> 2m.attributes = pm.posts.size ----> 4Is this a feature or a bug? If it is a feature, how is it supposed to be used?
With nested attributes, you'll wind up creating new records unless the entries in :posts_attributes have an :id key which indicates they're existing records to be updated.
--Matt Jones
-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/KT4-djio61EJ.
For more options, visit https://groups.google.com/groups/opt_out.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment