Browse files

Clear transaction state if callback raise rollback

  • Loading branch information...
1 parent e3d223b commit 535f299df9b8228c854d3ed0554feeaf1946812f @arthurnn arthurnn committed May 15, 2014
Showing with 14 additions and 1 deletion.
  1. +1 −1 activerecord/lib/active_record/transactions.rb
  2. +13 −0 activerecord/test/cases/transactions_test.rb
View
2 activerecord/lib/active_record/transactions.rb
@@ -328,7 +328,7 @@ def with_transaction_returning_status
begin
status = yield
rescue ActiveRecord::Rollback
- @_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) - 1
+ clear_transaction_record_state
status = nil
end
View
13 activerecord/test/cases/transactions_test.rb
@@ -123,6 +123,19 @@ def @first.after_save_for_transaction
assert !Topic.find(1).approved?
end
+ def test_rolling_back_in_a_callback_rollbacks_before_save
+ def @first.before_save_for_transaction
+ raise ActiveRecord::Rollback
+ end
+ assert !@first.approved
+
+ Topic.transaction do
+ @first.approved = true
+ @first.save!
+ end
+ assert !Topic.find(@first.id).approved?, "Should not commit the approved flag"
+ end
+
def test_raising_exception_in_nested_transaction_restore_state_in_save
topic = Topic.new

0 comments on commit 535f299

Please sign in to comment.