Derek
Derek

Reputation: 12378

Rails transactions within transactions?

What happens when you have transactions with transactions

def a
  ActiveRecord::Base.transaction do
    # stuff
  end
end

ActiveRecord::Base.transaction do
  a
  # more stuff
end

What happens if the inner transaction succeeds but the outer fails and vice versa? What happens when both succeed or fail?

Upvotes: 8

Views: 6054

Answers (2)

Shobhit_Geek
Shobhit_Geek

Reputation: 599

You can use nested transaction in rails.But you have to focus on the rollbacking part in case of nested transaction. Check nested-transations-in-rails .

Upvotes: -1

mechanicalfish
mechanicalfish

Reputation: 12826

transaction calls can be nested. By default, this makes all database statements in the nested transaction block become part of the parent transaction.

The behaviour is well described in the documentation

Upvotes: 11

Related Questions