Search code examples
ruby-on-rails

Using acts_as_paranoid plugin for soft delete - what about validations?


I am looking on trying to use acts_as_paranoid plugin for soft delete of records. I was earlier managing it using a flag in the db. I know that this plugin will omit a record from searches and finds if the record is soft deleted. What I want to know is if I have a validation in the model like validates_uniqueness_of :email and I deleted(soft deleted) the record having email '[email protected]'. Now when I try to create a new user having same email, will the validation work and prevents the creation of the new record. Or will it omit the soft deleted record as it does for finds? (I would like this to happen, of course.)


Solution

  • acts_as_paranoid does not reimplement validates_uniqueness_of, so if you have (soft) deleted a record with email '[email protected]' you cannot create a new record with the same email.

    The easy fix for this is to add a scope to validates_uniqueness_of:

    validates_uniqueness_of :email, :scope => :deleted_at
    

    This way you can have any number of (soft) deleted records with email '[email protected]' and still create a new record with the same email.