Rails 7.0 Added ActiveRecord::Base#previously_persisted? method

June 07, 2021

Rails 7 introduced a new method to ActiveRecord::Base called previously_persisted?.

This method checks whether a deleted ActiveRecord instance was previously existing in database.

Let's see this method in action:

## Before user = User.create!(email: 'info@scriptday.com') user.destroy! user.new_record? && user.destroyed? #=> true ## After new_user = User.new new_user.destroy! new_user.previously_persisted? #=> false existing_user = User.create!(email: 'info@scriptday.com') User.exists?(existing_user.id) #=> true existing_user.destroy! existing_user.previously_persisted? #=> true

previously_persisted? method is just a replacement for
!existing_user.new_record? && existing_user.destroyed?

ActiveRecord::Base#previously_persisted? was added with this pull request.

Got something to share with us ? Please inbox


© All rights reserved 2021