aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/transactions.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/transactions.rb')
-rw-r--r--activerecord/lib/active_record/transactions.rb29
1 files changed, 14 insertions, 15 deletions
diff --git a/activerecord/lib/active_record/transactions.rb b/activerecord/lib/active_record/transactions.rb
index 5ee644c0b4..77a8a14a2a 100644
--- a/activerecord/lib/active_record/transactions.rb
+++ b/activerecord/lib/active_record/transactions.rb
@@ -126,28 +126,27 @@ module ActiveRecord
end
def save_with_transactions(perform_validation = true) #:nodoc:
- rollback_active_record_state { transaction { save_without_transactions(perform_validation) } }
+ rollback_active_record_state! { transaction { save_without_transactions(perform_validation) } }
end
def save_with_transactions! #:nodoc:
- rollback_active_record_state { transaction { save_without_transactions! } }
+ rollback_active_record_state! { transaction { save_without_transactions! } }
end
-
- # stores the current id and @new_record values so that they are reset
- # after rolling the transaction back.
- def rollback_active_record_state
+
+ # Reset id and @new_record if the transaction rolls back.
+ def rollback_active_record_state!
+ id_present = has_attribute?(self.class.primary_key)
+ previous_id = id
previous_new_record = @new_record
- previous_id = self.id
- response = yield
- rescue
- response = false
- raise
- ensure
- unless response
- @new_record = previous_new_record
+ yield
+ rescue Exception
+ @new_record = previous_new_record
+ if id_present
self.id = previous_id
+ else
+ @attributes.delete(self.class.primary_key)
end
- response
+ raise
end
end
end