重现步骤

创建模型X并将其添加到模型文件中:

after_create_commit :do_stuff
after_update_commit :do_stuff


def do_stuff
  puts "doing stuff"
end

运行rails控制台:

rails c

创建一个新的X.

X.create(title: "Blah")

预期的行为

我们希望运行after_create_commit回调并打印出“doing stuff”。

实际行为

after_create_commit回调没有运行,并且没有打印出“doing stuff”。

系统配置

Rails版本:
5.0.4 
Ruby版本:
2.3.1



调用after_update_commit 确实会覆盖添加的回调after_create_commit,因为在引擎盖下它们都设置了after_commit具有不同条件的回调。

但是,after_*_commit别名看起来像是作为不同的回调实现的,这可能会让人感到困惑,因为它可能被认为是一个错误。

为after_create_commit和after_update_commit回调添加文档 

来自:after_create_commit and after_update_commit alias don't always run when next to each other


让其生效的写法

这不起作用:

after_create_commit :foo
after_update_commit :foo, if: :bar?

但这确实有效:

after_create_commit -> { foo }
after_update_commit -> { foo }, if: :bar?
1条评论 顺序楼层
请先登录再回复