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?