Forum: Ruby on Rails Does AR::Base#save update the entire object tree on update?

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.
Wes G. (Guest)
on 2007-01-20 01:07
On update (new_record? == false), if I call save on an AR::Base
descendant, is it guaranteed that the entire object graph is traversed
and each of the children is saved?

What I'm seeing is give an arbitrarily complex object graph of children
which belong to an object, when I save the object, the only objects
which get saved automatically are the children which are specified using
has_one, not any of the children specified using has_many.

Thanks,
Wes
Mark Reginald J. (Guest)
on 2007-01-20 13:44
(Received via mailing list)
Wes G. wrote:

> On update (new_record? == false), if I call save on an AR::Base
> descendant, is it guaranteed that the entire object graph is traversed
> and each of the children is saved?
>
> What I'm seeing is give an arbitrarily complex object graph of children
> which belong to an object, when I save the object, the only objects
> which get saved automatically are the children which are specified using
> has_one, not any of the children specified using has_many.

New has_many children will be automatically saved (created).
This will continue down the association levels as long as
there's an unbroken line of un-saved children.

Existing children are not updated.

--
We develop, watch us RoR, in numbers too big to ignore.
Wes G. (Guest)
on 2007-01-21 01:08
Mark Reginald J. wrote:

> New has_many children will be automatically saved (created).
> This will continue down the association levels as long as
> there's an unbroken line of un-saved children.
>
> Existing children are not updated.

Mark,

This is helpful.  I also found this post
(http://www.ruby-forum.com/topic/74822) from the core list which implies
that the automatic saving of has_one elements occurs as I've seen.

Upon further reflection, I see why one _might_ not want automatic saving
of children.  But in general, it seems like all the children should be
saved or none of them should be saved.

Anyone have any idea if the auto-saving of has_one associations will be
disabled in the future?

In the meantime, I will never assume that an object update will save
it's children and handle it myself, in case this auto-saving behavior
for has_one goes away.

Thanks,
Wes
This topic is locked and can not be replied to.