Parent.children not updated (has_many)


#1

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