Skip to content

Commit

Permalink
Fix new_record? and id rollback. Closes #6910.
Browse files Browse the repository at this point in the history
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5886 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
jeremy committed Jan 12, 2007
1 parent 353238b commit 3b6555a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
2 changes: 1 addition & 1 deletion activerecord/CHANGELOG
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


* Bring the sybase adapter up to scratch for 1.2 release. [jsheets] * Bring the sybase adapter up to scratch for 1.2 release. [jsheets]


* Rollback #new_record? and #id values for created records that rollback in an after_save callback. Closes #6910 [Ben Curren] * Rollback new_record? and id when an exception is raised in a save callback. #6910 [Ben Curren, outerim]


* Pushing a record on an association collection doesn't unnecessarily load all the associated records. [Obie Fernandez, Jeremy Kemper] * Pushing a record on an association collection doesn't unnecessarily load all the associated records. [Obie Fernandez, Jeremy Kemper]


Expand Down
29 changes: 14 additions & 15 deletions activerecord/lib/active_record/transactions.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -126,28 +126,27 @@ def destroy_with_transactions #:nodoc:
end end


def save_with_transactions(perform_validation = true) #:nodoc: 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 end


def save_with_transactions! #:nodoc: def save_with_transactions! #:nodoc:
rollback_active_record_state { transaction { save_without_transactions! } } rollback_active_record_state! { transaction { save_without_transactions! } }
end end


# stores the current id and @new_record values so that they are reset # Reset id and @new_record if the transaction rolls back.
# after rolling the transaction back. def rollback_active_record_state!
def rollback_active_record_state id_present = has_attribute?(self.class.primary_key)
previous_id = id
previous_new_record = @new_record previous_new_record = @new_record
previous_id = self.id yield
response = yield rescue Exception
rescue @new_record = previous_new_record
response = false if id_present
raise
ensure
unless response
@new_record = previous_new_record
self.id = previous_id self.id = previous_id
else
@attributes.delete(self.class.primary_key)
end end
response raise
end end
end end
end end
4 changes: 3 additions & 1 deletion activerecord/test/transactions_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -133,8 +133,9 @@ def test_callback_rollback_in_create
:content => "Have a nice day", :content => "Have a nice day",
:approved => false) :approved => false)
new_record_snapshot = new_topic.new_record? new_record_snapshot = new_topic.new_record?
id_present = new_topic.has_attribute?(Topic.primary_key)
id_snapshot = new_topic.id id_snapshot = new_topic.id

# Make sure the second save gets the after_create callback called. # Make sure the second save gets the after_create callback called.
2.times do 2.times do
begin begin
Expand All @@ -146,6 +147,7 @@ def test_callback_rollback_in_create
assert_equal "Make the transaction rollback", e.message assert_equal "Make the transaction rollback", e.message
assert_equal new_record_snapshot, new_topic.new_record?, "The topic should have its old new_record value" assert_equal new_record_snapshot, new_topic.new_record?, "The topic should have its old new_record value"
assert_equal id_snapshot, new_topic.id, "The topic should have its old id" assert_equal id_snapshot, new_topic.id, "The topic should have its old id"
assert_equal id_present, new_topic.has_attribute?(Topic.primary_key)
ensure ensure
remove_exception_raising_after_create_callback_to_topic remove_exception_raising_after_create_callback_to_topic
end end
Expand Down

0 comments on commit 3b6555a

Please sign in to comment.