How To Configure Paranoia Gem With Ruby on Rails

Yashu Mittal

Paranoia is a gem that allows you to hide records instead of destroying them. You can recover them later if you want. It’s a very simple reimplementation of ActsAsParanoid.

I assume you have a Rails app with User, Conversation and Message models created, with basic views added with conversations showing authors and messages

We will use paranoia gem.



Step 1

Add paranoia gem to your Gemfile:

gem 'paranoia', git: '', branch: 'core'

You add it like that because the newest release doesn’t include some features that will be used in this tutorial.

Step 2

Now, let’s say that you want to delete an user, but his messages should still be displayed with his name as an author (in your view you’ll have something like If you removed that user, when trying to display his messages you would get an error:

undefined method `name` for nil:NilClass

In order to avoid that, instead of destroying records you should mark them as deleted. It’s called soft deleting and it’s what paranoia gem is all about.

To enable this behaviour for User model, add this line to it:

class User < ActiveRecord::Base

You also need to generate and run this migration:

rails generate migration AddDeletedAtToUsers deleted_at:datetime:index
rake db:migrate

From now on, calling destroy on any user will set deleted_at to a current date instead of actually destroying the record.

Step 3

But, if you try to softly delete an user and display his messages, you’ll still get the same error as before. That’s because paranoia gem changes the default_scope of a model, so every query will treat softly deleted records as if they weren’t there (WHERE “users”.”deleted_atIS NULL will be added to queries). Of course if you love your default_scope and you don’t ever want it to change you can achieve that:

# not available in the newest release, you have to add paranoia gem as in step 1
acts_as_paranoid without_default_scope: true

You can also access your softly deleted user record in other ways:

If you need to include or ignore softly deleted records in other places, there are several ways to do that in addition to the methods above:

Note that if you want your indexes to ignore softly deleted records, you have to modify them as follows:

add_index :users, :some_column, where: "deleted_at IS NULL"

Step 4

If you want to restore a user, use this code:




Step 5

What if you want to softly delete an user and all of his messages? You can do that simply by implementing acts_as_paranoid for Message model (as in step 2, you also need to add a migration) and then using dependent: :destroy

class User < ActiveRecord::Base
  has_many :messages, dependent: :destroy

Note that if you didn’t implement acts_as_paranoid for Message model the messages would be destroyed normally and you wouldn’t be able to restore them. Also, restoring the user doesn’t restore all of his messages unless you use:

User.restore(user_id, recursive: true)


user.restore(recursive: true)

Response to “How To Configure Paranoia Gem With Ruby on Rails”

Stay current

Sign up for our newsletter, and we'll send you news and tutorials on business, growth, web design, coding and more!