There's no explicit hook, but you can pretty much do what you've described using transactions. If you're updating the chapters in a single controller action, you can use a transaction block (see http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html) to wrap all the changes. Then, either use an after_save on Book, or just call a method directly to validate the combination.
You'll want to use save! and friends within the block, and catch exceptions (ActiveRecord::RecordInvalid and ActiveRecord::RecordNotSaved) to display errors. --Matt Jones On Jan 24, 2009, at 6:44 PM, Greg Hauptmann wrote: > Hi Mike, all > > Understood. To help align my fictitious example to the cross-model > validation question I've asked consider that: > (a) the book value is fixed [e.g. perhaps think of this as a bank > account transaction amount, being allocated out to different tax > categories & then the user wants to adjust the tax categories] > (b) the user manually adjusts the chapter value (i.e. there is no > programmatic approach to calculating the distribution) > > So this brings it back to my scenario I'm not sure how to solve in > Rails whereby the sequence of events here would be: > - change chapter 1 value > - change chapter 2 value > - change chapter 3 value > - <only at this point should the cross model business rule be > checked, i.e. Book.amount.should == Sum(chapter values)> > > My assumption here (correct me if I'm wrong) is that any Rails > validation/after_save/observer kicks in at such of the sequence > points, whereas what is actually required here is a cross_model > business logic check at the end. > > Does this make sense? Is there a ways in Rails to get access to a > "before_commit" type hook that would align with the point I want the > business logic check to kick in? > > Thanks > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
