Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear the transaction state when AR object is duped #31751

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
* Clear the transaction state when AR object is duped.

Fixes #31670.

*Yuriy Ustushenko*

* Support for PostgreSQL foreign tables.

*fatkodima*
Expand Down
6 changes: 4 additions & 2 deletions activerecord/lib/active_record/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,10 @@ def initialize_dup(other) # :nodoc:

_run_initialize_callbacks

@new_record = true
@destroyed = false
@new_record = true
@destroyed = false
@_start_transaction_state = {}
@transaction_state = nil

super
end
Expand Down
16 changes: 16 additions & 0 deletions activerecord/test/cases/dup_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "cases/helper"
require "models/reply"
require "models/topic"
require "models/movie"

module ActiveRecord
class DupTest < ActiveRecord::TestCase
Expand Down Expand Up @@ -157,5 +158,20 @@ def test_dup_without_primary_key
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