aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRoque Pinel <repinel@gmail.com>2015-07-19 22:00:36 -0400
committerSean Griffin <sean@thoughtbot.com>2015-07-20 09:12:01 -0600
commit12b0b26df7560ab5199ba830586864085441508f (patch)
treefc2cc53a6795ee099fec878e4c33f7337f05b476 /activerecord
parentc0ef95a1c6db3095c4b5f80f8044fbbbdfebeff1 (diff)
downloadrails-12b0b26df7560ab5199ba830586864085441508f.tar.gz
rails-12b0b26df7560ab5199ba830586864085441508f.tar.bz2
rails-12b0b26df7560ab5199ba830586864085441508f.zip
Fix state being carried over from previous transaction
This clears the transaction record state when the transaction finishes with a `:committed` status. Considering the following example where `name` is a required attribute. Before we had `new_record?` returning `true` for a persisted record: ```ruby author = Author.create! name: 'foo' author.name = nil author.save # => false author.new_record? # => true ```
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md14
-rw-r--r--activerecord/lib/active_record/transactions.rb4
-rw-r--r--activerecord/test/cases/transactions_test.rb7
3 files changed, 25 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index aa2438fb3f..d9aa703d19 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,17 @@
+* Fix state being carried over from previous transaction.
+
+ Considering the following example where `name` is a required attribute.
+ Before we had `new_record?` returning `true` for a persisted record:
+
+ author = Author.create! name: 'foo'
+ author.name = nil
+ author.save # => false
+ author.new_record? # => true
+
+ Fixes #20824.
+
+ *Roque Pinel*
+
* Correctly ignore `mark_for_destruction` when `autosave` isn't set to `true`
when validating associations.
diff --git a/activerecord/lib/active_record/transactions.rb b/activerecord/lib/active_record/transactions.rb
index 267ac26c79..887d7a5903 100644
--- a/activerecord/lib/active_record/transactions.rb
+++ b/activerecord/lib/active_record/transactions.rb
@@ -379,6 +379,10 @@ module ActiveRecord
raise ActiveRecord::Rollback unless status
end
status
+ ensure
+ if @transaction_state && @transaction_state.committed?
+ clear_transaction_record_state
+ end
end
protected
diff --git a/activerecord/test/cases/transactions_test.rb b/activerecord/test/cases/transactions_test.rb
index 4e163ca4a6..29a6ec7522 100644
--- a/activerecord/test/cases/transactions_test.rb
+++ b/activerecord/test/cases/transactions_test.rb
@@ -175,6 +175,13 @@ class TransactionTest < ActiveRecord::TestCase
assert topic.new_record?, "#{topic.inspect} should be new record"
end
+ def test_transaction_state_is_cleared_when_record_is_persisted
+ author = Author.create! name: 'foo'
+ author.name = nil
+ assert_not author.save
+ assert_not author.new_record?
+ end
+
def test_update_should_rollback_on_failure
author = Author.find(1)
posts_count = author.posts.size