Mayuresh
Mayuresh Full stack developer working on Ruby on Rails and React

Soft Delete in Ruby on rails

Soft Delete in Ruby on rails

Building a web app that can perform basic crud operations can be easily done with Rails.

soft delete making the record appear to be deleted while keeping the actual record in the database

this can easily be done by adding a flag column to check the column while querying that particular database

rails provide various implementations for soft deleting records

1) Paranoia

2) discard

PARANOIA :-

paranoia was a reimplementation of acts_as_paranoid rails plugin

it has the ability to hide and restore database records

suppose we are using acts_as_paranoid in a model and performing destroy on it will not delete the actual record instead of that

it will add the current time in the deleted_at column and hides the records by scoping all the queries on your model which do not have a deleted_at field

Installation:-

1
gem 'paranoia'

then

1
bundle install

then after that run a migration that will ad. deleted_at field in the particular model in which you want to soft delete records

1
bin/rails generate migration AddDeletedAtToUsers deleted_at:datetime:index

and then you will have a migration like this

1
2
3
4
5
6
class AddDeletedAtToUsers < ActiveRecord::Migration
  def change
    add_column :users, :deleted_at, :datetime
    add_index :users, :deleted_at
  end
end

after that

1
rails db:migrate

Usage:-

suppose you have a model User, add acts_as_paranoid

1
2
3
4
5
class User < ActiveRecord::Base
  acts_as_paranoid

  # ...
end

now calling delete or destroy will now set deleted_at column

suppose you want to delete the entry from the database also then you need to call

1
really_destroy!

this will hard delete(delete record from table)

Discard:-

It is an ActiveRecord mixin to add flagging for records that are discarded

installation:-

1
gem 'discard'

then

1
bundle install

Usage:-

1
2
3
class Article < ActiveRecord::Base
  include Discard::Model
end

then you can either generate a migration like:

1
rails generate migration add_discarded_at_to_articles discarded_at:datetime:index

or create manually

1
2
3
4
5
6
class AddDiscardToArticles < ActiveRecord::Migration[5.0]
  def change
    add_column :posts, :discarded_at, :datetime
    add_index :posts, :discarded_at
  end
end

then you need to make a change in the controller

just replace destroy with discard

1
2
3
4
def destroy
  @article.discard
  redirect_to article_url, notice: "Article removed"
end

For New Projects you can go with Discard instead of using paranoia!!!!

comments powered by Disqus