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.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---