Skip to content

Commit a8709e6

Browse files
authored
Merge pull request rails#53669 from a5-stable/copy-previously_new_record-at-becomes
Ensure previously_new_record? is preserved in #becomes
2 parents c11312a + f4e9224 commit a8709e6

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

activerecord/lib/active_record/persistence.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ def becomes(klass)
492492
becoming.instance_variable_set(:@attributes, @attributes)
493493
becoming.instance_variable_set(:@mutations_from_database, @mutations_from_database ||= nil)
494494
becoming.instance_variable_set(:@new_record, new_record?)
495+
becoming.instance_variable_set(:@previously_new_record, previously_new_record?)
495496
becoming.instance_variable_set(:@destroyed, destroyed?)
496497
becoming.errors.copy!(errors)
497498
end

activerecord/test/cases/persistence_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,17 @@ def test_becomes_includes_changed_attributes
460460
assert_equal %w{name}, client.changed
461461
end
462462

463+
def test_becomes_preserve_record_status
464+
company = Company.new(name: "37signals")
465+
client = company.becomes(Client)
466+
assert_predicate client, :new_record?
467+
468+
company.save
469+
client = company.becomes(Client)
470+
assert_predicate client, :persisted?
471+
assert_predicate client, :previously_new_record?
472+
end
473+
463474
def test_becomes_initializes_missing_attributes
464475
company = Company.new(name: "GrowingCompany")
465476

0 commit comments

Comments
 (0)