paranoia or acts_as_paranoid
paranoia is a re-implementation of
acts_as_paranoid for Rails 3/4/5, using much, much, much less code.
When your app is using Paranoia, calling destroy on an ActiveRecord object doesn’t actually destroy the database record, but just hides it. Paranoia does this by setting a deleted_at field to the current time when you destroy a record, and hides it by scoping all queries on your model to only include records which do not have a deleted_at field.
paranoia gem according to the RubyGem latest update:
|GitHub latest update||RubyGem latest update||Comments|
|paranoia||Now||2.4.3 - December 16, 2020||Paranoia will continue to accept bug fixes and support new versions of Rails but isn’t accepting new features.|
|acts_as_paranoid||Now||0.7.0 - August 29, 2020|
You can install it as a gem:
gem install paranoia
or add it into a Gemfile (Bundler):
bin/rails generate migration AddDeletedAtToUsers deleted_at:datetime:index
Show the migration 20200811215130_add_deleted_at_to_users.rb.
class AddDeletedAtToUsers < ActiveRecord::Migration
Run db migrate.
class User < ActiveRecord::Base
By default, ActsAsParanoid assumes a record’s deletion is stored in a datetime column called
If a record is deleted by ActsAsParanoid, it won’t be retrieved when accessing the database.
So, Paranoiac.all will not include the deleted records.
When you want to access them, you have 2 choices:
User.only_deleted # retrieves only the deleted records
Watch out for these caveats:
You cannot use scopes named with_deleted and only_deleted
You cannot use scopes named deleted_inside_time_window, deleted_before_time,
deleted_after_time if your paranoid column’s type is time
You cannot name association *_with_deleted
unscoped will return all records, deleted or not