-
Notifications
You must be signed in to change notification settings - Fork 21.9k
Add :conditions option to uniqueness validator #5321
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 :conditions option to uniqueness validator #5321
Conversation
@@ -35,8 +35,8 @@ def validate_each(record, attribute, value) | |||
relation = relation.and(table[scope_item].eq(scope_value)) | |||
end | |||
|
|||
if finder_class.unscoped.where(relation).exists? | |||
record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope).merge(:value => value)) | |||
if finder_class.unscoped.where(relation).where(options[:conditions]).exists? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would prefer only to call the .where(options[:conditions])
if conditions are present - but didn't want to move too much around in this first take. Suggestions?
Thanks @pfeiffer. I believe something along these lines is convenient but I would rather allow a scope like |
@josevalim Great - I've pushed an update supporting |
@@ -102,6 +108,14 @@ module ClassMethods | |||
# validates_uniqueness_of :teacher_id, :scope => [:semester_id, :class_id] | |||
# end | |||
# | |||
# It is also possible to limit the uniqueness constraint to a set of records matching certain conditions. | |||
# In this example archived articles with are not being taken into consideration when validating uniqueness |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the unwanted "with" please.
@vijaydev Done! |
@josevalim Have you had time to review the changes? |
Add :conditions option to uniqueness validator
Merged, sorry for the delay. It somehow escaped my inbox. :) |
@jonleighton hey, given AR is moving to a lazy approach for everything - from scopes to associations - I guess this should be changed to follow the same approach, wdyt? |
@carlosantoniodasilva yeah I guess so. |
@pfeiffer hey, perhaps you want to take a look on that? Otherwise let me know and I'll try to take care of it later this week. Thanks! |
This is a follow up to rails#5321 and follows the general direction in AR to make things lazy evaluated.
Uniqueness validation uses a proc to specify the `:conditions` option. This is a follow up to #5321 and follows the general direction in AR to make things lazy evaluated.
This commit adds a
:conditions
option to the uniqueness validator. The:conditions
is added to the lookup used for determining the uniqueness of the attribute.Example use-case:
An article with a
title
and astatus
. Status can be eitherdraft
,published
orarchived
. We want to have unique titles - but only for non-archived articles. This cannot be accomplished using:scope => :status
, since a draft article and a published article with identical titles both would be valid. This can now be accomplished with:In this case a draft article can have the same title as an archived article, but not the same name as a published article.
What do you think? Let me know if anything is missing - this is my first pull request to Rails.