after_create有多种方法?

时间:2013-01-18 02:29:59

标签: ruby-on-rails ruby-on-rails-3 rails-activerecord

我试图在创建之后调用两个方法,但是将它们放入数组中是行不通的...我在rails docs或google中找不到任何东西......有经验的人吗?

after_create [:do_this, :do_that]

不起作用

4 个答案:

答案 0 :(得分:73)

无需包围数组中的方法。只需使用:

after_create :do_this, :and_then_this

奖励信息: 如果before_*回调返回false,则取消所有后续回调和相关操作。如果after_*回调返回false,则取消所有后续回调。回调通常按照定义的顺序运行,但定义为模型上的方法的回调除外,这些回调最后被调用。

答案 1 :(得分:2)

为什么要将两个回调方法放入数组中?

after_create:do_this,:do_that

答案 2 :(得分:0)

您还可以调用一个方法并在该方法上安排顺序。

after_create :process


def process
  do_this
  do_that
  then_this
end

我个人更喜欢这种方法。没有隐藏的魔法。

答案 3 :(得分:0)

请注意,after_action似乎是倒过来的。


after_action :do_that_after, :do_that_before

当前执行订单为do_that_before,然后为do_that_after

编辑:使用Rails 6.0.3.3