Implementing acts_as_paranoid gem (soft delete) in rails app — step by step
Accidentally deleted some record, no worries, now you can get it back.🎉
We all come across the situation where we accidentally delete some record and then want them back but can’t. Here comes the acts_as_paranoid
gem. A Rails plugin to perform soft delete on records.
In simpler words, the record gets hidden instead of deleted.
Lets start with an easy implementation:
Prerequisites:
We will target following versions:
ruby 2.7+
rails 5.2+
For older ruby/rails versions
Please refer: https://github.com/ActsAsParanoid/acts_as_paranoid/tree/0-6-stable
https://github.com/ActsAsParanoid/acts_as_paranoid/tree/rails3.0
https://github.com/ActsAsParanoid/acts_as_paranoid/tree/rails3.1
https://github.com/ActsAsParanoid/acts_as_paranoid/tree/rails4.0
https://github.com/ActsAsParanoid/acts_as_paranoid/tree/support-jruby-3-4
1. Add Gem to your Gemfile:
gem 'acts_as_paranoid'
2. Run bundle install on terminal:
bundle install
=> Now, We will need an extra column in the table for which we want to soft delete the record.
According to rails conventions we will name it as "deleted_at" with "datetime" type.
Say we have a Model Article and want to soft delete article records when they are destroyed.
Add a migration to add column deleted_at to articles table.
3. Run following command on terminal:
rails g migration AddDeletedAtToArticle deleted_at:datetime:index
4. Run the migration by following command on terminal:
rails db:migrate
OUTPUT:== 20230904082311 AddDeletedAtToArticle: migrating ============================
-- add_column(:articles, :deleted_at, :datetime)
-> 0.0011s
-- add_index(:articles, :deleted_at)
-> 0.0391s
== 20230904082311 AddDeletedAtToArticle: migrated (0.0403s) ===================
5. Add "acts_as_paranoid" in your model file. Here in
app/models/article.rb
class Article < ApplicationRecord
acts_as_paranoid
#Write your other code below as per requirement
end
Hooray 🎉 That’s it. We are ready to delete our article record.
Go to terminal.
If you don’t have records of article then create one with necessary fields. I have 2 columns for my article “title” and “body”. So I will run
Article.create(title: "My Article", body: "Welcome to my Ruby on Rails Blog.")
Now we will soft delete this object. It can be done in the same way we delete any object by using “destroy” or “delete” method.
Article.last.destroy
Above command will update the "deleted_at" column with current date and time instead of actually deleting the object from database.Article.all will not retrieve this deleted article.
That’s all.✌️ ___Don’t forget to take your bonus tips below😉___
Special Thanks 💐 to my Mentor Mr. Pankaj Dhote (Sr. ROR Developer) who guided me in all directions.Enjoyed reading this article? Give it a round of applause by clapping 👏 and share your thoughts in the comments section below.
S
ome Bonus Tips :
- Deleted records can be retrieved by
Article.only_deleted (get only deleted articles),
Article.with_deleted (get all articles including deleted articles) - Deleted record can be restored by “recover” method
Article.only_deleted.last.recover - To permanently delete object you can use “destroy_fully!”
or you can use “destroy” twice on the same object. - For more customizations please refer https://github.com/ActsAsParanoid/acts_as_paranoid/tree/master
Thank You Folks.