Forum: Ruby on Rails Saving ActiveRecord objects that are composed of other objec

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.
John W. Long (Guest)
on 2006-01-13 18:37
(Received via mailing list)
I've got two pretty simple model objects Page and PagePart. Page
has_many PageParts and PageParts belong_to Pages.

When updating PageParts for a specific Page I'd like to be able to do:

   page.parts[x].content = 'changed'
   page.save

and expect that should the validation pass the changed page part would
be saved as well, but this doesn't seem to work for me. Why doesn't
ActiveRecord automatically update objects that are composed of other
objects when saving?

I'd rather not write:

   if page.save
     page.parts.each { part.save }
   end

because should a failure occur when saving the parts I don't want the
page to be saved. Of course I could override the #save method and use
transactions for this, but I'd rather not do that if ActiveRecord
provides something built in. Am I missing something?

--
John L.
http://wiseheartdesign.com
This topic is locked and can not be replied to.