-
-
Notifications
You must be signed in to change notification settings - Fork 909
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support multiple uniq validations on same attr
It turns out that you can have multiple uniqueness validations on the same attribute. This is useful if you want the attribute to be unique under different sets of scopes. For instance: ``` class CalibrationConstantVersion < ActiveRecord::Base validates_uniqueness_of( :calibration_constant, scope: [:name] ) validates_uniqueness_of( :calibration_constant, scope: [:begin_at, :physical_device_id] ) end ``` So if you have a test like this: ``` describe CalibrationConstantVersion do it do should validate_uniqueness_of(:calibration_constant). scoped_to(:name). case_insensitive end it do should validate_uniqueness_of(:calibration_constant). scoped_to(:begin_at, :physical_device_id) end end ``` the first test will fail because the matcher doesn't know that :calibration_constant is supposed to be unique relative to :begin_at and :physical_device_id, and the second test will fail because it doesn't know :calibration_constant is supposed to be unique relative to :name. Now, in order to properly fix this, we'll need to add another matcher. However, for the time being we *can* fix the following case: ``` class CalibrationConstantVersion < ActiveRecord::Base validates_uniqueness_of( :calibration_constant, scope: [:name], message: 'first message' ) validates_uniqueness_of( :calibration_constant, scope: [:begin_at, :physical_device_id], message: 'second message' ) end describe CalibrationConstantVersion do it do should validate_uniqueness_of(:calibration_constant). scoped_to(:name). with_message('first message') end it do should validate_uniqueness_of(:calibration_constant). scoped_to(:begin_at, :physical_device_id). with_message('second message') end end ``` These tests also fail, but for a different reason. One of the checks we make is to compare the list of scopes you've provided with the list of scopes that are actually on the validation. Notice I said "validation" -- we don't expect there to be multiple, we just look for the first uniqueness validation that's on the attribute in question. So in this case, the first test will pass, but the second test will fail, because it thinks all that :calibration_constant is scoped to is :name.
- Loading branch information
Showing
3 changed files
with
130 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters