【发布时间】:2010-09-07 16:39:59
【问题描述】:
我正在使用新的 Rails 3 API,我对新方法 run_callbacks(kind, *args, &block) 有疑问
在以下代码中:
class User < ActiveRecord::Base
before_save :say_hi
after_save :say_bye
private
def say_hi; puts "hi"; end
def say_bye; puts "bye"; end
end
我可以通过运行在保存时显式调用 回调:
> u.run_callbacks(:save)
hi
bye
=> true
但是我的问题是,我怎样才能仅运行 before_save 或 after_save 回调?
查看run_callbacks(kind, *args, &block)代码:
# File activesupport/lib/active_support/callbacks.rb, line 92
def run_callbacks(kind, *args, &block)
send("_run_#{kind}_callbacks", *args, &block)
end
我不知道如何构建 *args 只调用 before 或 after 回调,我尝试了类似 u.run_callbacks(:before_save) 的东西(给我未定义的方法错误) u.run_callbacks(:save, :before) 运行所有 save 回调(before 和 after)。
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-3