Skip to content
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

Add options for uniqueness validations. Resolves #319 #333

Open
wants to merge 1 commit into
base: core
Choose a base branch
from

Conversation

rbr
Copy link
Contributor

@rbr rbr commented Jun 12, 2016

For with_default_scope add a paranoia: :with_deleted option when you want to ensure uniqueness on all records.
For without_default_scope add a paranoia: :without_deleted option when you want to ensure uniqueness on only non-deleted records.

@rbr
Copy link
Contributor Author

rbr commented Jun 12, 2016

This will break behaviour for people already using the without_default_scope option together with a uniqueness validation. Currently it will exclude deleted records, after this merge it will include them, and you'd need to use the paranoia: :without_deleted option to get the same behaviour again .

Wasn't sure if we should handle that case as the without_default_scope option hasn't been put into an official version yet.

@denys281
Copy link

denys281 commented May 19, 2017

@BenMorganIO @jhawthorn Could we hope that it sometimes will be merged?

@nghiabk
Copy link

nghiabk commented Sep 19, 2017

Don't custom paranoid gem.You can write:
validates :name, uniqueness: {conditions: ->{with_deleted}}

class Client < ActiveRecord::Base
acts_as_paranoid

validates :name, uniqueness: { paranoia: :with_deleted }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't custom paranoid gem.You can write:
validates :name, uniqueness: {conditions: ->{with_deleted}}

class Client < ActiveRecord::Base
acts_as_paranoid without_default_scope: true

validates :name, uniqueness: { paranoia: :without_deleted }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't custom paranoid gem.You can write:
validates :name, uniqueness: {conditions: ->{with_deleted}}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't custom paranoid gem.You can write:
validates :name, uniqueness: {conditions: ->{with_deleted}}

Doesn't work for me

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't work for me either in rails v4.2.10 using MySQL. Traced it a bit and I found that rails wasn't actually removing the scope via the unscope method because of the way the relation query is set up. By the time it hits here, the relation is wrapped and chained with an AND query. It doesn't get through this case statement to actually remove the column from the scope because the relation rel is a Arel::Nodes::And at that point.

I just ended up making my own validator which manually checked for uniqueness against the unscoped records to move forward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants