Matt/Ryan/Adam - thanks. I think I'd need to ponder further your suggestions and do some tests to let things sink in. In the meantime to answer some of your questions:
My Goal - Clarify whether Rails could be used (via validation/before_save/after_save type hooks) to provide a solid protection for a cross-model business rule. That is to protect the developer for making a mistake and for example accidentally making changes (in his/her code) that could lead to business rule violation (i.e. assuming they didn't change the validation/before_create/after_create checks themselves). I think in summary what I'm hearing is: (a) It's not really possible, however (b) It is possible to achieve protection for the business rule if the developer follows an appropriate approach to making updates/changes, i.e. such that an appropriate validation would kick in if necessary. Am I correct here? Thanks On Tue, Jan 27, 2009 at 1:50 AM, Adam <[email protected]> wrote: > > Greg, > > If I understand the problem correctly, you want to update each of the > Allocations for a Book in one request, making sure that by the end of > the request the value sums are still valid. Correct? Based on that > assumption, I'm guessing that you're trying to do something like this > (in, I'm also guessing, your BooksController): > > def update > # explicitly open a transaction > # (maybe) update values for specified Book > # save Book > # for each Attribute > # update values > # save > #end > # check Attribute sum validations and rollback transaction if > validation fails, commit otherwise > # end transaction > end > > The problem with this is that you're doing a lot of unnecessary > database work in the case where validation fails. And Rails doesn't > really support this approach easily. However, you have all the > information you need in order to do validation before any database > saves. You can build your HTML form to generate params such that the > modifications to Allocations get passed to the Book object on > update_attributes. The Book model then updates its Allocations, > checks the sum validation, and then (in an after_save callback) saves > all of its associated Allocations. If validation fails, you get a run- > of-the-mill Rails validation error response without touching the > database. > > On a related note, this validation strikes me as something that you > might want to consider doing on the client side, since you have all > the necessary information available there, and it could end up being > much simpler there. > > > -- Greg http://blog.gregnet.org/ --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
