Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Browse files

Fallback when previous_value doesn't respond to #next.

For example, floating point attributes won't respond to #next, so in
this case fall back to adding 1. This is not perfect, but it seems
to rarely occur in practice since I wasn't able to find anyone else
complaining about it.
  • Loading branch information...
commit 9d0c74e4bfc2a1abb509cc4bdc1756ffdb561b9d 1 parent 3d94390
@sjmadsen sjmadsen authored
6 lib/shoulda/matchers/active_model/validate_uniqueness_of_matcher.rb
@@ -116,7 +116,11 @@ def validate_after_scope_change
# Assume the scope is a foreign key if the field is nil
previous_value ||= 0
- next_value =
+ next_value = if previous_value.respond_to?(:next)
+ else
+ previous_value + 1
+ end
@subject.send("#{scope}=", next_value)

5 comments on commit 9d0c74e


If previous value doesn't respond to next, how about calling to_s and then calling next?

"1.25".next # "1.26"

That works great, too. I committed and pushed that change. Should I send a pull request?


Yep, that sounds great. We usually go through open-source pull requests on Friday


I have been hit by this problem too. I been trying a sligthly different workaround:

next_value = @subject.class.order(scope)

This also solve the case when next_value is already in the database. I am not sure if it's a better solution because it make validate_uniqueness_of ActiveRecord specific. I would like to hear your thoughts about this.


I minimize the amount of data in my tables for test runs and do not use fixtures. When these tests run, there is only one record in the database: the one I saved explicitly to test the uniqueness requirement. The simpler fall-back for next_value is enough in this case.

Please sign in to comment.
Something went wrong with that request. Please try again.