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

Fix #previously_new_record? on destroyed records #48796

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions 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
Expand Down
4 changes: 3 additions & 1 deletion activerecord/lib/active_record/persistence.rb
Expand Up @@ -661,7 +661,7 @@ def new_record?
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
Expand Down Expand Up @@ -760,6 +760,7 @@ def save!(**options, &block)
def delete
_delete_row if persisted?
@destroyed = true
@previously_new_record = false
freeze
end

Expand All @@ -775,6 +776,7 @@ def destroy
destroy_associations
@_trigger_destroy_callback ||= persisted? && destroy_row > 0
@destroyed = true
@previously_new_record = false
freeze
end

Expand Down
8 changes: 8 additions & 0 deletions activerecord/test/cases/base_test.rb
Expand Up @@ -968,6 +968,14 @@ def test_previously_new_record_returns_boolean
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?
Expand Down