了解Rails 4中的after_update回调
我有一个带有after_update
回调的Rails对象, after_update
记录发送到队列。 问题是我注意到有时队列的处理速度比对象实际更新的速度要快。
我的问题:在对象更新结束后调用after_update
,但是当它开始时? 如果我想在更新成功后才想用它做什么,我需要什么回调?
在事务块中调用after_save
, after_create
, after_update
,因此它们将在执行SQL语句之前执行。
如果要在语句执行完成时执行某些操作,则应使用after_commit回调。
如果您查阅Rails文档,您会发现许多可以使用的回调。 这项工作的最佳选择可能是“after_commit”:
这直接来自Rails Docs(底部的链接)
3.1 Creating an Object before_validation after_validation before_save around_save before_create around_create after_create after_save after_commit/after_rollback 3.2 Updating an Object before_validation after_validation before_save around_save before_update around_update after_update after_save after_commit/after_rollback 3.3 Destroying an Object before_destroy around_destroy after_destroy
Rails DOcs: http ://guides.rubyonrails.org/active_record_callbacks.html