Flowdock

Notes posted by giosakti

RSS feed
June 1, 2011 - (>= v3.0.0)
0 thanks

Catching rollback and re-raise exception

In response to wiseleyb, I don’t believe that you could put “rescue” in a transaction block, let alone catching ActiveRecord::Rollback. It would lead you to an “unexpected kRESCUE” error.

I think this is more appropriate.

def start_transaction
  Company.transaction do
    # don't forget the bang to make sure it raise
    # exception or the transaction won't rollback
    user.save!
    company.save!
    x=1/0

    return true
  end

  # re-raise exception here
  raise "Exception!"
end

Then you could call the method in another place, and it would raise rollback and other exception.

...
  # would return the "Exception!" if rollback occurs
  # it would also still trigger another exception other
  # than rollback.
  start_transaction
...