我有以下代码,我想在ActiveRecord的on_new_annoucement
触发时触发FooClass的回调。我已经使回调数组成为模块的类变量,但它超出了Activerecord的回调范围。
module FooModule
class FooClass
attr_reader :callbacks
def initialize
@@callbacks = Hash.new { |h, k| h[k] = [] }
end
def on_new_announcement(&block)
@@callbacks[:on_new_announcement] << block
end
end
class Announcement < ActiveRecord::Base
after_save :on_new_announcement
def on_new_announcement
....
end
end
end