-
Notifications
You must be signed in to change notification settings - Fork 529
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can I use uniqueness validation for all records, include deleted. #319
Comments
@ALL @davidpiegza @radar is there any progress in this issue? I am facing same problem.
Or is there any ideas to resolve this issue? |
@gkunwar You'll have to use partial indexes. Sadly, MySQL doesn't support them. You're SoL on that unless you move from MySQL to something like PostgreSQL. Then you can do: # pg only
add_index :users, :email, unique: true, where: 'deleted_at IS NOT NULL` |
@dmitrychopey see #333 |
Thank you @BenMorganIO for the response. |
Currently the validation for uniqueness runs on non deleted records. Is it possible to tell the rails validation to run on all records in order to properly validate the uniqueness. Some use cases allow for multiple entries of the same object as long as only one is active and the rest are deleted. In my particular use case for email address I only ever want one record. If a user deleted a record and tries to add it again, I want the validation for uniqueness to fail so I can give them the option to restore the record. Any ideas please? |
I solved it "undoing" what paranoia does(check this https://github.com/rubysherpas/paranoia/blob/core/lib/paranoia.rb#L299). Briefly, Paranoia implements UniquenessParanoiaValidator and overwrites What I have done is overwrite UniquenessParanoiaValidator#build_relation method again but this time with its original implementation. Code:
I hope it helps, regards |
I have been having a similar issue but I want to be able to selectively include deleted records. I have a working solution (I think) similar to the one above. I created a new validator in
Then in my model I can use something like **UPDATE: This does not work in Rails 5.0, I am using the solution from @nghiabk 👍 ** |
I validated uniqueness email in my project when we used gem paranoia. Remember in gem paranoia, with_deleted is available |
In my application I need to validate email uniqueness for all records, not only not deleted.
The text was updated successfully, but these errors were encountered: