有一种方法可以同样处理`after_save`和`after_destroy`“吗?

我正在使用Rails 3.1.0,我想知道是否可以“同等”处理after_saveafter_destroy回调。 也就是说,我需要为after_saveafter_destroy回调运行相同的方法。

此时我必须单独处理这些回调,即使这些回调完成相同的事情:

 after_save do |record| # Make a thing end after_destroy do |record| # Make the same thing as in the 'after_save' callback end 

那么, 有一种方法可以“同等”地处理after_saveafter_destroy吗?

而不是块给after_destroyafter_destroy一个模型的方法名称作为符号。

 class ModelName < AR after_save :same_callback_method after_destroy :same_callback_method def same_callback_method # do the same for both callbacks end end 
 class Foo < ActiveRecord::Base after_save :my_callback after_destroy :my_callback private def my_callback #Do stuff end end 

要在保存和销毁后执行相同的回调,可以使用after_commit

 after_commit do |record| # Is called after creating, updating, and destroying. end 

http://apidock.com/rails/ActiveRecord/Transactions/ClassMethods/after_commit