Rails has provided methods like
exists?to check whether the object is present in the database table or not.
persisted?to check whether the object is not a new record and it is not destroyed from the database.
But no method tells us that the particular object was a part of the database table in the past and now is deleted.
Before Rails 7:
Let’s say we have a
Customer model with the
It has a row with the name “John Doe”.
If we have deleted this entry from the table, we can verify that “John Doe” was our previous customer or not using the following code snippet:
In Rails 7:
As per the discussion in this
Rails 7 has added a method
We can now use the
previously_persisted? method on the object directly,
instead of the above conditions.
This method is helpful in the case of the
where we are performing some cleanup activities for associated records.
Check out the PR for more details.