xop
2013-Feb-26 19:35 UTC
attributes= operation *appends* instead of replacing for accepts_nested_attributes_for with has_many
When using "attributes=" for an object with a accepts_nested_attributes_for a model that has a has_many relationship will cause the has_many array to be appended-to instead of being replaced as would be expected by a straight assignment operation. See this example: class Member < ActiveRecord::Base has_many :posts accepts_nested_attributes_for :posts end class Post < ActiveRecord::Base belongs_to :member end m=Member.new p={:name => ''joe'', :posts_attributes => [{:title=>''t1''},{:title=>''t2''}]} m.attributes = p m.posts.size ----> 2 m.attributes = p m.posts.size ----> 4 Is this a feature or a bug? If it is a feature, how is it supposed to be used? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/Oz8JeQi5ja0J. For more options, visit https://groups.google.com/groups/opt_out.
Frederick Cheung
2013-Feb-26 22:30 UTC
Re: attributes= operation *appends* instead of replacing for accepts_nested_attributes_for with has_many
On Tuesday, February 26, 2013 7:35:23 PM UTC, xop wrote:> When using "attributes=" for an object with a accepts_nested_attributes_for a model > that has a has_many relationship will cause the has_many array to be appended-to > instead of being replaced as would be expected by a straight assignment operation. > >attributes= has always behaved like that ( the name is bad - it''s not really an accessor at all. The newer name is assign_attributes) Fred> See this example: > > > class Member < ActiveRecord::Base > has_many :posts > accepts_nested_attributes_for :posts > end > > > class Post < ActiveRecord::Base > belongs_to :member > end > > > m=Member.new > p={:name => ''joe'', :posts_attributes => [{:title=>''t1''},{:title=>''t2''}]} > > > m.attributes = p > m.posts.size ----> 2 > > > m.attributes = p > m.posts.size ----> 4 > > > Is this a feature or a bug? If it is a feature, how is it supposed to be used?-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/94kv3wlP3SkJ. For more options, visit https://groups.google.com/groups/opt_out.
Matt Jones
2013-Feb-28 14:06 UTC
Re: attributes= operation *appends* instead of replacing for accepts_nested_attributes_for with has_many
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 model > that has a has_many relationship will cause the has_many array to be > appended-to > instead of being replaced as would be expected by a straight assignment > operation. > > See this example: > > class Member < ActiveRecord::Base > has_many :posts > accepts_nested_attributes_for :posts > end > > class Post < ActiveRecord::Base > belongs_to :member > end > > m=Member.new > p={:name => ''joe'', :posts_attributes => [{:title=>''t1''},{:title=>''t2''}]} > > m.attributes = p > m.posts.size ----> 2 > > m.attributes = p > m.posts.size ----> 4 > > Is 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.