aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorYuriy Ustushenko <yoreek@yahoo.com>2018-01-20 16:55:31 +0700
committerYuriy Ustushenko <yoreek@yahoo.com>2018-01-25 10:09:16 +0700
commit226116a44010f4da857f6b26d73bec2618dea890 (patch)
tree893eb97d975eaf335efcccecd19cac2c48278365 /activerecord
parentf1a30d8ad5ae17e6202fd3a126027378773dcaea (diff)
downloadrails-226116a44010f4da857f6b26d73bec2618dea890.tar.gz
rails-226116a44010f4da857f6b26d73bec2618dea890.tar.bz2
rails-226116a44010f4da857f6b26d73bec2618dea890.zip
Clear the transaction state when AR object is duped
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md6
-rw-r--r--activerecord/lib/active_record/core.rb6
-rw-r--r--activerecord/test/cases/dup_test.rb16
3 files changed, 26 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index e2dc8045e2..41a9c8cc89 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,9 @@
+* Clear the transaction state when AR object is duped.
+
+ Fixes #31670.
+
+ *Yuriy Ustushenko*
+
* Support for PostgreSQL foreign tables.
*fatkodima*
diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb
index 2c65f618dc..e1a0b2ecf8 100644
--- a/activerecord/lib/active_record/core.rb
+++ b/activerecord/lib/active_record/core.rb
@@ -382,8 +382,10 @@ module ActiveRecord
_run_initialize_callbacks
- @new_record = true
- @destroyed = false
+ @new_record = true
+ @destroyed = false
+ @_start_transaction_state = {}
+ @transaction_state = nil
super
end
diff --git a/activerecord/test/cases/dup_test.rb b/activerecord/test/cases/dup_test.rb
index 73da31996e..59c3331595 100644
--- a/activerecord/test/cases/dup_test.rb
+++ b/activerecord/test/cases/dup_test.rb
@@ -3,6 +3,7 @@
require "cases/helper"
require "models/reply"
require "models/topic"
+require "models/movie"
module ActiveRecord
class DupTest < ActiveRecord::TestCase
@@ -157,5 +158,20 @@ module ActiveRecord
record.dup
end
end
+
+ def test_dup_record_not_persisted_after_rollback_transaction
+ movie = Movie.new(name: "test")
+
+ assert_raises(ActiveRecord::RecordInvalid) do
+ Movie.transaction do
+ movie.save!
+ duped = movie.dup
+ duped.name = nil
+ duped.save!
+ end
+ end
+
+ assert !movie.persisted?
+ end
end
end