'is there a way to set after_commit callbacks to a specific ActiveRecord transaction

I am creating a ActiveRecord transaction this way

ActiveRecord::Base.transaction do
  MyModel.create!(name: "value")
  OtherModel.create!(name: "value")
end

I'd want to set a specific after_commit callback to this transaction. I don't want to attach it to any one of the models because I don't it to be executed every time I create one of these separately.

I cannot simply write some code in the end of the transaction block either, because maybe the real transaction is created outside of this code (I am not setting the require_new option to true)

Is there any way to set callbacks to a specific transaction instance?

thank you very much



Solution 1:[1]

Please take a look at these gems:

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Zoran