diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 5ad5321451c..67a66ad8148 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -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 diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index 93c3cc1c6c5..c69f43dec21 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -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 diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 9461bff9d61..de88d36031a 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -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?