ruby on rails - Receiving already loaded object from find_by method of ActiveRecord::Associations::CollectionProx -
i have following models
class parent < activerecord::base has_many :children end class child < activerecord::base belongs_to :parent end
what want changing child object parent.children.find_or_initialize_by()
, modify child object, save parent.save
. children doesn't saved. here's code.
# prepare parent child p = parent.new({:name=>'parent'}) p.children << child.new({:name => 'child'}) p.save # modify , save child p = parent.first c = p.children.find_or_initialize_by({:name=>'child'}) #=> returns child created above c.name = 'new child' p.save #=> children aren't saved
i know because find_by
method returns new object rather 1 loaded. seems more natural me if code above works because method invoked through activerecord::associations::collectionproxy
. missing work? or there reasons find_by
on activerecord::associations::collectionproxy
have work does?
(i know can save modified child c.save
, want save child through parant.save
, because want errors on child object referenced parent.errors.)
Comments
Post a Comment