我正在以这种方式创建一个 ActiveRecord 事务
ActiveRecord::Base.transaction do
MyModel.create!(name: "value")
OtherModel.create!(name: "value")
end
我想为此事务设置一个特定的after_commit
回调。我不想将它附加到任何一个模型,因为我不想在每次单独创建其中一个模型时都执行它。
我也不能简单地在 transaction
block 的末尾写一些代码,因为也许真正的交易是在这段代码之外创建的(我没有设置 require_new
选项为 true
)
有什么方法可以设置回调到特定的事务实例吗?
非常感谢
最佳答案
请看看这些 gem :
- after_commit_everywhere (免责声明:我是作者)
- after_transaction_commit
关于ruby-on-rails - 有没有办法将 after_commit 回调设置为特定的 ActiveRecord 事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42505547/