allow_nil option for validate_uniqueness_of #129

JulianKniephoff opened this Issue Aug 30, 2010 · 10 comments


None yet
10 participants

I added an option that checks whether the validation of uniqueness is skipped, when the attribute in question has a value of nil. So basically it tries to enforce something along the lines of

validates_uniqueness_of :attr, :allow_nil => true # (a)

The branch can be found here:

validate_uniqueness_of with allow_nil option given now matches models having such a clause in them. It does not match a model with just

validates_uniqueness_of :attr # (b)

as it does not allow nil duplicates. A class with statement (a) on the other hand is no longer matched by just validate_uniqueness_of (without allow_nil option) as it allows a duplicate: nil. Accordingly, validates_uniqueness_of without allow_nil does (still) match a model with (b) in it.

In summary:

validates_uniqueness_of :attr, :allow_nil => true (Model) validate_uniqueness_of(:attr).allow_nil (Test) matches?
no no yes
no yes no
yes no no
yes yes yes

I hope this behavior conforms with Shoulda.

any chance to have this merged in - if not, any reason why?

jbasdf commented Oct 10, 2011

I'd like to see this get merged in as well.

+1 for merging this in

shaliko commented Nov 15, 2011


one more!



gabebw commented Mar 18, 2012


This looks useful - can you file a pull request over at shoulda-matchers? That's where all of the "real code" lives now. Shoulda has become just a meta-gem that depends on that code.

-- Gabe

gabebw closed this Mar 18, 2012

I'm gonna look into that as soon as possible.

any update on this? would be a cool feature!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment