如何从spec或rails控制台调用类,after_create方法

时间:2012-09-13 18:24:29

标签: ruby-on-rails ruby

我有以下

class Item < ActiveRecord::Base
    after_create TellMe
end

class TellMe
  def self.after_create(model)
end

并希望能够做类似的事情:

i=Item.new :name => 'my test name'
i.send(:TellMe.after_create)

类似于我如何在公共方法上调用send?看起来我可以做到

i.instance_eval 'TellMe.after_create(self)'

但感觉有点难看(除其他外)

1 个答案:

答案 0 :(得分:1)

触发回调的唯一方法是执行限定事件,在这种情况下,创建项目。

作为您想要的解决方法,您可以创建另一种方法,该方法将完全执行回调操作,并且您可以像正常一样访问它

Class Item < ActiveRecord::Base

  def tellme(params)
    TellMe.function(params)
  end

end