Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

validate_uniqueness_of matcher should be able to allow nil #96

gabebw opened this Issue Apr 11, 2012 · 5 comments


None yet
5 participants

gabebw commented Apr 11, 2012

Something like should { validate_uniqueness_of(:attribute).allow_nil }. Looks like it may take a bit of work - for example, if the existing_value is nil, then what do we do? Set it to abc? Have allow_nil take a parameter?


jferris commented Apr 11, 2012

This requires an existing record with nil for the unique column. You can currently do this with allow_value:

context "with an existing record" do
  before { FactoryGirl.create(:record, :column => nil) }

  it "allows multiple rows to have nil for column" do
    should allow_value(nil).for(:column)

Wrapping it up more succinctly would be nice, though.

I finally got around to translating my old fork of shoulda implementing this feature (thoughtbot/shoulda#129 (comment)) to shoulda-matchers. It can be found here: https://github.com/JulianKniephoff/shoulda-matchers/tree/validate_uniqueness_of_allow_nil

However it is broken with Rails 3.1 (tests don't pass) probably because of this: rails/rails#5853

nisley commented Jun 15, 2012



gabebw commented Dec 27, 2012

Closing in favor of #182.

@gabebw gabebw closed this Dec 27, 2012

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