From 3b6555acd4516167f5ca094eeae50bf5ffc59db9 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 12 Jan 2007 05:10:06 +0000 Subject: Fix new_record? and id rollback. Closes #6910. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5886 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/lib/active_record/transactions.rb | 29 +++++++++++++------------- 1 file changed, 14 insertions(+), 15 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 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 -- cgit v1.2.3