Setting up using a simple example: I have 1 table ( Totals ), which contains the sum of the amount column of each record in the second table ( Things ).
When thing.amount updated, I would just add the difference between the old value and the new value in total.sum .
Now I subtract self.amount during before_update and add self.amount during after_update . This causes WAY to trust the update too much.
Limitation: I do not want to simply recount the sum of all transactions.
Question: I just want to get the original value during the after_update . What were your ways of doing this?
Update: I will go with the idea of Luke Frank. During the after_update , you still have access to the self.attr_was values, which are exactly what I wanted. I also decided to go with the after_update implementation, because I want to save such a model in the model. Thus, no matter how I decide to update transactions in the future, I will know that I am updating the transaction amount correctly. Thank you all for your implementation recommendations.
callback ruby-on-rails activerecord model
Abel Mar 03 '09 at 16:19 2009-03-03 16:19
source share