From 1af2022cc32b604529a5ac3a85a3bd92a3c42936 Mon Sep 17 00:00:00 2001 From: Rick Olson Date: Tue, 2 Jan 2007 05:36:30 +0000 Subject: Rollback #new_record? and #id values for created records that rollback in an after_save callback. Closes #6910 [Ben Curren] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5830 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/lib/active_record/transactions.rb | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'activerecord/lib/active_record/transactions.rb') diff --git a/activerecord/lib/active_record/transactions.rb b/activerecord/lib/active_record/transactions.rb index 6a776966bb..5ee644c0b4 100644 --- a/activerecord/lib/active_record/transactions.rb +++ b/activerecord/lib/active_record/transactions.rb @@ -126,11 +126,28 @@ module ActiveRecord end def save_with_transactions(perform_validation = true) #:nodoc: - transaction { save_without_transactions(perform_validation) } + rollback_active_record_state { transaction { save_without_transactions(perform_validation) } } end def save_with_transactions! #:nodoc: - 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 + previous_new_record = @new_record + previous_id = self.id + response = yield + rescue + response = false + raise + ensure + unless response + @new_record = previous_new_record + self.id = previous_id + end + response end end end -- cgit v1.2.3