Saving ActiveRecord objects that are composed of other objec


#1

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