0
我正在玩run_callbacks並出現問題。有人可以幫我嗎?Rails 3 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
有什麼更好的想法嗎?