-
Type: Improvement
-
Resolution: Fixed
-
Priority: Minor - P4
-
Affects Version/s: None
-
Component/s: Docs, Validations
-
Fully Compatible
One can use :conditions option to validates_uniqueness_of to limit the scope of validation:
Dictionary.validates_uniqueness_of(:name, conditions: -> { Dictionary.where(:year.gte => 1900, :year.lt => 2000) })
This allows dictionary to be created with years 950 and 1950:
Dictionary.create!(name: "French-English", year: 950) Dictionary.create!(name: "French-English", year: 1950)
... but not the other way around:
Dictionary.create!(name: "French-English", year: 1950) Dictionary.create!(name: "French-English", year: 950)
One might expect the specified conditions to be applied to the model instance being validated, but this is not the case: conditions only apply to the database query. For avoidance of confusion, this behavior should be documented.
AR works the same way as Mongoid.
- is related to
-
MONGOID-5393 Investigate and fix behavior of validates_uniqueness_of :conditions option
- Closed
- related to
-
MONGOID-3946 validates uniqueness of a field with scope not working
- Closed
- links to