Bug fix: uniqueness validation fails with scoped attributes of data type time #1190
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR to fix issue #1114
The bug
The validation fails but it should pass when the scope has data type
time
.Example: model
Schedule
with the following attributes and their data types:day_of_week
:integer
start_time
:time
Spec:
This bug is caused by the method that calculates the next value to test for the uniqueness of the scope:
lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb
When
previous_value
is of typetime
:previous_value
->2018-07-02 11:20:14
previous_value.to_datetime.next
->2018-07-03 11:20:14
The date is incremented by 1, however the time remains the same. As a result when the next value is set on the new record the uniqueness validation fails since the 2 records have the same time value.
The fix
This PR fixes the bug by adding
in(60)
to increment the timestamp by 60 second before incrementing the date withnext
:previous_value.to_datetime.in(60).next