Adding it to your model is straightforward. Given you want to add soft-deletion to your Client model, first run
rails generate migration AddDeletedAtToClients deleted_at:datetime:index to add the
deleted_at column in the
clients table. Then add
acts_as_paranoid to your Client model, like in the following snippet:
class Client < ActiveRecord::Base acts_as_paranoid end
From now on,
Client#destroy sets the current datetime in the
deleted_at column, while
Client default scope returns only records which have a nil value for
To really destroy a client, you should use
To list all clients (even the deleted ones) you can use
Client.with_deleted, while you can use
Client.only_deleted to get only deleted records.
Paranoia even follows relationships. So if you are in the following scenario:
class Client < ActiveRecord::Base has_many :addresses, dependent: :destroy acts_as_paranoid end class Address < ActiveRecord::Base belongs_to :client acts_as_paranoid end
when you call
Client#destroy paranoia will soft-delete all its addresses.
The only problem I found after my setup was with the uniqueness validation. In this scenario:
class Client < ActiveRecord::Base acts_as_paranoid validates :name, :uniqueness => true end
the uniqueness validator will check among deleted records too, and in my case I did want to check only among not-deleted records.
For this purpose there is a gem called (paranoia_uniqueness_validator)[https://github.com/anthonator/paranoia_uniqueness_validator] that can be used this way:
class Client < ActiveRecord::Base acts_as_paranoid validates :name, :uniqueness_without_deleted => true end
And it works as expected.
So, easy soft-deletion for all 🙂