Forum: Ruby on Rails parent.children not updated (has_many)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Christian Schlack (Guest)
on 2006-04-15 13:45
(Received via mailing list)
Hi guys!

class Parent < ActiveRecord::Base
   has_many :children
end

class Child < ActiveRecord::Base
   belongs_to :parent
end

I do:

parent = Parent.find(1, :include => :children)
parent.children.find(3).update_attribute('name', 'Tom') (or
parent.children.find(3).name = 'Tom')

parent.children.find(3).name
   => 'old value'

The database is updated correctly, SELECT name FROM children WHERE id=3
AND parent_id=1
   => 'Tom'

Only the array parent.children doesn't seem to get updated. I cannot
force reloading (parent.cildren(true).find(3).name would do the trick)
because I would possibly lose unsaved children.

How can I update the parent.children array?

- Christian
This topic is locked and can not be replied to.