Simple way to Implement Soft Delete Active Record feature in Ruby on Rails

Total Blog Views: 130

Blog Status: publish

Created By: nandini Created at: 08-17-2021

Tags: ruby on rails soft delete ruby on rails rails Expert India

Ruby on Rails help do build amazing feature in simpler way. We can easily perform basic Creation and deletion processes in rails.Commonly required feature nowadays is to keep our database records backup.That means records can be deleted for the User interface but should be available in the database.In the future we can restore actual data as per requirement.

 

In general, soft delete can be handled  by adding a deleted flag column and keep checking every time whenever there is a transaction in the table database.

This will be a disorganised way, if we need some customization in the soft delete process , we will have to write extra code.

While implementing this for the first time I found one of the best gem that was fulfilling my requirements. Paranoia  gem allows to implement activerecord soft delete feature. Record marked as deleted_at timestamp.

It has a simple integration process.

 

Install gem:  Add gem in Gemfile

gem "paranoia"
   

Use the gem version based on the rails version. Run bundle install

Add deleted_at to Model
Add deleted_at field in the model in which soft delete requires.
Ex: User model require soft delete  add deleted_at column in User model

Migration:

rails generate migration AddDeletedAtToUsers deleted_at:datetime:index

Run the migration:

bundle exec rails db:migrate

Model changes:
Update model by adding acts_as_paranoid to the User model Ex: 

class User < ApplicationRecord
  acts_as_paranoid
end

Check Soft Delete:

When we destroy any User, the record will be soft deleted by updating deleted_at column of user model to current timestamp.

user = User.first
user.destroy

Scopes provided by Paranoia

Paranoia gem provides some scopes to fetch the soft deleted records.

with_deleted : This scope can be used if we want to search through deleted records as well.

 

Ex: User.with_deleted.where(post_id: 1)

without_deleted: Scope can be used if we don’t require deleted active records to fetch while query execution.

User.without_deleted.where(post_id: 1)
unscoped: Rails provides this one to exclude all the scopes applied to ActiveRecord relation when querying.
 

 There are some instance methods available

deleted? : Returns true/false if the record is deleted.
restore(ids) : Accepts ids to be restored and marks deleted_at column nil.

Thanks For Reading, If Having Question and Suggestion Please Let me know In comments


nandini

I am nandini blogger on shadbox. I am influencer,content writer,author and publisher. Feel free to ask me any question and suggestions.



Comments



  • asad | almost 4 years ago
    Nice information
  • nandini | almost 4 years ago
    I will add Some More Information , If required Please do let me know
  • swazahmad | almost 4 years ago
    Very good and information noted for future use
    • nandini | almost 4 years ago
      Thank You so much for the feedkback
Buy traffic for your website

About Shadbox

we have the “Get things executed” lifestyle at our place of work. There are not any excuses, no if’s or however’s in our dictionary. committed to navigating the ship of creativity to create cell answers, we resolve the real-lifestyles troubles of our clients and their clients. Our passion for work has won us many awards, year after 12 months.

Services

Downloads