Skip to content

Add options for uniqueness validations. Resolves #319#333

Open
rbr wants to merge 1 commit into
rubysherpas:corefrom
rbr:add_options_for_uniqueness_validation
Open

Add options for uniqueness validations. Resolves #319#333
rbr wants to merge 1 commit into
rubysherpas:corefrom
rbr:add_options_for_uniqueness_validation

Conversation

@rbr
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown

denys281 commented May 19, 2017

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

@nghiabk
Copy link
Copy Markdown

nghiabk commented Sep 19, 2017

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

Comment thread README.md
class Client < ActiveRecord::Base
acts_as_paranoid

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

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}}

Comment thread README.md
class Client < ActiveRecord::Base
acts_as_paranoid without_default_scope: true

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

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}}

Copy link
Copy Markdown

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
Copy Markdown

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.

rioug added a commit to rioug/openfoodnetwork that referenced this pull request Nov 13, 2024
Paranoia doesn't support unique validation including deleted records:
  rubysherpas/paranoia#333
We use a custom validator, ScopedUniquenessValidator to avoid the issue
rioug added a commit to rioug/openfoodnetwork that referenced this pull request Nov 13, 2024
Paranoia doesn't support unique validation including deleted records:
  rubysherpas/paranoia#333
We use a custom validator, ScopedUniquenessValidator to avoid the issue
mkllnk pushed a commit to rioug/openfoodnetwork that referenced this pull request Nov 26, 2024
Paranoia doesn't support unique validation including deleted records:
  rubysherpas/paranoia#333
We use a custom validator, ScopedUniquenessValidator to avoid the issue
RachL pushed a commit to rioug/openfoodnetwork that referenced this pull request Nov 28, 2024
Paranoia doesn't support unique validation including deleted records:
  rubysherpas/paranoia#333
We use a custom validator, ScopedUniquenessValidator to avoid the issue
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.

7 participants