Bubbling Up Nested-transaction Failures with ActiveRecord

前端 未结 3 1870
北海茫月
北海茫月 2020-12-29 22:51

I have a method in a service object that composes operations that should be wrapped in a transaction. Some of these operations are also wrapped in transactions. For example:

3条回答
  •  佛祖请我去吃肉
    2020-12-29 23:05

    Another easier approach, you can raise an CustomError < StandardError. Unlike ActiveRecord::Rollback, Error will bubble up through parent transactions. You can handle it wherever required. All other inner transactions will be rolled back.

    Try the below template.

    ActiveRecord::Base.transacton do
        ActiveRecord::Base.transacton do
            raise StandardError.new
        end
    end
    

提交回复
热议问题