我正在实施resque以在模型实例被销毁后重新计算某些东西。为此,我使用after_destroy
回调。在我实现计算之前,我想测试以确保计算将在不同的子集上执行。
所以为了测试这个,我正在使用rails c --sandbox
并且收到一些令人困惑的信息。这是输出:
> @run = Run.new(status: 0, archived: false)
> @run.save
> I then print the Run.count and get 200
> @run.destroy!
> I then print the Run.count and get 200
注意我使用after_save
和after_destroy
打印出我认为它们应该不同的Run.count数字。
我的最终目标是在某些条件发生变化时,将计算添加到resque队列并继续。
我的主要问题是。为什么数字没有改变?特定运行是否会被标记为删除而不包含在我的计算查询中?
答案 0 :(得分:1)
当回调执行时,它在事务中执行。在事务结束之前执行计算时,它可能会导致问题。尝试使用after_commit
回调,该回调在事务结束后执行。
after_commit :do_something, on: :create
after_commit :do_something, on: :destroy