使用 ActiveRecord,是否有一种方法可以在 after_update 期间获取记录的旧值

使用一个简单的示例进行设置: 我得到了1个表(Totals) ,其中包含第二个表(Things)中每条记录的 amount列之和。

当一个 thing.amount得到更新时,我想简单地将旧值和新值之间的差值添加到 total.sum

现在我在 before_update减去 self.amount,在 after_update加上 self.amount。这使得对后续更新过于信任。

约束: 我不想简单地重新计算所有事务的总和。

问: 很简单,我想在 after_update回调期间访问原始值。你有什么办法做到这一点?

更新: 我同意 Luke Francl 的观点。在 after_update回调期间,您仍然可以访问 self.attr_was值,这正是我想要的。我还决定使用 after_update实现,因为我想在模型中保留这种逻辑。这样,无论我将来决定如何更新事务,我都会知道我正在正确地更新事务的总和。感谢大家的实施建议。

61657 次浏览

Firstly, you should be doing this in a transaction to ensure that your data gets written together.

To answer your question, you could just set a member variable to the old value in the before_update, which you can then access in the after_update, however this isn't a very elegant solution.

Idea 1: Wrap the update in a database transaction, so that if the update fails your Totals table isn't changed: ActiveRecord Transactions docs

Idea 2: Stash the old value in @old_total during the before_update.

Add this to your model:

def amount=(new_value)
@old_amount = read_attribute(:amount)
write_attribute(:amount,new_value)
end

Then use @old_amount in your after_update code.

Ditto what everyone is saying about transactions.

That said...

ActiveRecord as of Rails 2.1 keeps track of the attribute values of an object. So if you have an attribute total, you will have a total_changed? method and a total_was method that returns the old value.

There's no need to add anything to your model to keep track of this anymore.

Update: Here is the documentation for ActiveModel::Dirty as requested.

Some other folks are mentioning wrapping all this in a transaction, but I think that's done for you; you just need to trigger the rollback by raising an exception for errors in the after_* callbacks.

See http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

The entire callback chain of a save, save!, or destroy call runs within a transaction. That includes after_* hooks. If everything goes fine a COMMIT is executed once the chain has been completed.

If a before_* callback cancels the action a ROLLBACK is issued. You can also trigger a ROLLBACK raising an exception in any of the callbacks, including after_* hooks. Note, however, that in that case the client needs to be aware of it because an ordinary save will raise such exception instead of quietly returning false.

ActiveRecord::Dirty is a module that's built into ActiveRecord for tracking attribute changes. So you can use thing.amount_was to get the old value.

To get all changed fields, with their old and new values respectively:

person = Person.create!(:name => 'Bill')
person.name = 'Bob'
person.save
person.previous_changes        # => {"name" => ["Bill", "Bob"]}

Appending "_was" to your attribute will give you the previous value before saving the data.

These methods are called dirty methods methods.

Cheers!

If you want to get value of particular field after update you can use field_before_last_save method.

Example:


u = User.last
u.update(name: "abc")


u.name_before_last_save will give you old value (before update value)

From Rails 5.1, the behavior of attribute_was inside of after callbacks have change. attribute_was will return the value after the save is done and return the current value in an after_save or after_update callback.

attribute_before_last_save is invoked two ways to get the previous value of a field in the after_save and after_update callbacks right now:

Option #1

attribute_before_last_save('yourfield')

Option #2

*_before_last_save