Fix #previously_new_record? on destroyed records

Ref: #48794

`#previously_new_record?` should return false for records that are
created and then destroyed.
This commit is contained in:
Adrianna Chang 2023-07-24 14:32:14 -04:00
parent d580a776c3
commit 7981988fa0
No known key found for this signature in database
GPG Key ID: 6816AFC60CB96DE5
3 changed files with 19 additions and 1 deletions

View File

@ -1,3 +1,11 @@
* Fix `#previously_new_record?` to return true for destroyed records.
Before, if a record was created and then destroyed, `#previously_new_record?` would return true.
Now, any UPDATE or DELETE to a record is considered a change, and will result in `#previously_new_record?`
returning false.
*Adrianna Chang*
* Specify callback in `has_secure_token`
```ruby

View File

@ -661,7 +661,7 @@ module ActiveRecord
end
# Returns true if this object was just created -- that is, prior to the last
# save, the object didn't exist in the database and new_record? would have
# update or delete, the object didn't exist in the database and new_record? would have
# returned true.
def previously_new_record?
@previously_new_record
@ -760,6 +760,7 @@ module ActiveRecord
def delete
_delete_row if persisted?
@destroyed = true
@previously_new_record = false
freeze
end
@ -775,6 +776,7 @@ module ActiveRecord
destroy_associations
@_trigger_destroy_callback ||= persisted? && destroy_row > 0
@destroyed = true
@previously_new_record = false
freeze
end

View File

@ -968,6 +968,14 @@ class BasicsTest < ActiveRecord::TestCase
assert_equal false, Topic.find(1).previously_new_record?
end
def test_previously_new_record_on_destroyed_record
topic = Topic.create
assert_predicate topic, :previously_new_record?
topic.destroy
assert_not_predicate topic, :previously_new_record?
end
def test_previously_persisted_returns_boolean
assert_equal false, Topic.new.previously_persisted?
assert_equal false, Topic.new.destroy.previously_persisted?