我正在玩 run_callbacks 并且遇到了问题。有人可以帮帮我吗?
## loveable.rb
module Loveable
extend ActiveSupport::Concern
included do
define_callbacks :love
end
def loved_by!(lover)
run_callbacks :love do
do_love(lover)
end
end
def do_love(lover)
...implementation goes here...
end
end
## product.rb
class Product < ActiveRecord::Base
include Loveable
set_callback :after, :love, :after_love
def after_love
## How to get lover here??
end
end
我需要 after_love 方法中的“情人”用户。我怎么能做到这一点?
现在我使用实例变量,但不喜欢这个解决方案。
## loveable.rb
def loved_by!(lover)
@lover = lover
run_callbacks...
end
## product.rb
def after_love
@lover.do_something
end
有更好的主意吗?