我将提供最通用的解决方案。
即使gem声明了自定义回调,例如
paranoia和
after_real_destroy
,它也可以正常工作。
列出所有回调。
Model.methods.select { |m| m.to_s.include? "callback" }.sort
然后,如果您键入方法名称,例如,您可以获得给定的回调函数。
Model._update_callbacks
Model._real_destroy_callbacks
Model._destroy_callbacks
如果您列出所有回调函数,就可以通过检查
@filter
实例变量来找到所需的回调函数。
require 'pp'
Activity._destroy_callbacks.each_with_index { |clbk,index| puts "#{index}-------\n#{clbk.pretty_inspect}" } ; nil
#<ActiveSupport::Callbacks::Callback:0x00007ff14ee7a968
@chain_config=
{:scope=>[:kind, :name],
:terminator=>
#<Proc:0x00007ff13fb825f8@/Users/mypc/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/activemodel-4.1.16/lib/active_model/callbacks.rb:103 (lambda)>,
:skip_after_callbacks_if_terminated=>true},
@filter=
#<Proc:0x00007ff14ee7ac10@/Users/mypc/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/activerecord-4.1.16/lib/active_record/associations/builder/association.rb:135 (lambda)>,
@if=[],
@key=70337193825800,
@kind=:before,
@name=:destroy,
@unless=[]>
4-------
#<ActiveSupport::Callbacks::Callback:0x00007ff14ee3a228
@chain_config=
{:scope=>[:kind, :name],
:terminator=>
#<Proc:0x00007ff13fb825f8@/Users/mypc/.rbenv/versions/2.3.7/lib/ruby/gems/2.3.0/gems/activemodel-4.1.16/lib/active_model/callbacks.rb:103 (lambda)>,
:skip_after_callbacks_if_terminated=>true},
@filter=:audit_destroy,
@if=[],
@key=:audit_destroy,
@kind=:before,
@name=:destroy,
@unless=[]>
5-------