If you want to bail out of the whole thing if the parent or any of the children are not valid, just put them in an array as you loop through them and set their attributes, In a separate loop, relate them to the parent at the end. Then inside of the first loop, throw an exception or something to get you out of the transaction, and not save anything.
The only snag with this approach is if you validate that the children have a parent_id. If so, just set it brute force -- cild.parent_id = @parent.id matt On 7/26/07, Glenn Little <[EMAIL PROTECTED]> wrote: > I've got a form that collects data for a parent and a number > of child records. I'm trying to update all the records and > then do one @parent.save to transactionally save the whole > bunch, but am having trouble. > > My form gives me params[:parent] and params[:children] > The latter is a hash of hashes, each one containing the > info to find and fill out or modify one child. > The simplified version of the handler code is: > > --------------------------------------- > @parent = Parent.find(foo) > > @parent.attributes = params[:parent] > > params[:children].each_value do |child_hash| > child = Child.find(child_hash[:id]) > child.attributes = child_hash > ???????? > end > > if @parent.save > yay > end > --------------------------------------- > > The problem is that I need to know what goes at "????" > in order to associate the child objects with the parent such > that when I do the save, the children are updated as well. > > I can't use: > > @parent.children << child > > because that immediately saves the child. I want to delay > the save until the transaction. On the other hand, if I put > nothing at all there, then the children's updates are not saved. > > Thanks in advance. > > -glenn > > > _______________________________________________ > Sdruby mailing list > [email protected] > http://lists.sdruby.com/mailman/listinfo/sdruby > _______________________________________________ Sdruby mailing list [email protected] http://lists.sdruby.com/mailman/listinfo/sdruby
