after_create有多个方法?

问题描述 投票:29回答:3

我试图在after create中调用两个方法,但把它们放到一个数组中却无法工作......我在rails docs或google中找不到任何东西......有人有经验吗?

after_create [:do_this, :do_that]

不工作

ruby-on-rails ruby-on-rails-3 rails-activerecord
3个回答
80
投票

不需要把方法放在数组中,只需使用:[:do_this,...]。只需使用。

after_create :do_this, :and_then_this

奖励信息:如果a before_* 回调返回false,所有后面的回调和相关操作都会被取消。如果一个 after_* 回调返回false,所有后面的回调都被取消。回调一般按照定义的顺序运行,但定义为模型上的方法的回调除外,它是最后调用的。


2
投票

为什么要把这两个回调方法放到一个数组中?

After_create :do_this, :do_that


0
投票

你也可以调用一个单一的方法,并在方法上直接编排顺序。

after_create :process


def process
  do_this
  do_that
  then_this
end

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

© www.soinside.com 2019 - 2024. All rights reserved.