Using act_as_paranoid plugin for soft deletion - how about validation?

I am trying to use the act_as_paranoid plugin to gently delete records. I used to control it using the flag in db. I know that this plugin will omit the entry from the search and will find if the entry is softly deleted. I want to know if I have validation in the type model validates_uniqueness_of :emailand I deleted (gently deleted) the email entry " prince@gmail.com ". Now, when I try to create a new user with the same email address, verification will be performed and prevent the creation of a new entry. Or will he omit the soft remote record, as for finds? (I would like this to happen, of course.)

+3
source share
3 answers

acts_as_paranoiddoes not override validates_uniqueness_of, therefore, if you (gently) deleted the entry by email " prince@gmail.com ", you cannot create a new entry with the same email address.

An easy fix for this is to add scope for validates_uniqueness_of:

validates_uniqueness_of :email, :scope => :deleted_at

This way, you can have any number of (soft) deleted entries with the email address " prince@gmail.com " and still create a new entry with the same email address.

+12
source

From our testing, a fix that acts_as_paranoidaffects deletion, so you get two entries. Of most conversations on the Internet, this is probably what you expect.

. , "" , , . , , . acts_as_paranoid, - .

, (:with_deleted => true), . , . , , " " .

+2

yor "rails3_acts_as_paranoid", ,

ActiveRecord's built-in uniqueness check does not take into account records deleted by ActsAsParanoid. If you want to check the uniqueness of not only deleted records, use the validates_as_paranoid macro in your model. Then, instead of using validates_uniqueness_of, use validates_uniqueness_of_without_deleted. This will result in deleted records being counted from the uniqueness check.

You must specify the following method,

acts_as_paranoid
validates_as_paranoid
validates_uniqueness_of_without_deleted :name
0
source

Source: https://habr.com/ru/post/1791221/


All Articles