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

Validation: allow_nil & allow_blank don't work correctly #441

Closed
Zhomart opened this Issue Aug 1, 2012 · 1 comment

Comments

Projects
None yet
2 participants

Zhomart commented Aug 1, 2012

As ActiveRecord's validation says, (http://ar.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html)

:allow_nil - Skip validation if attribute is nil.
:allow_blank - Skip validation if attribute is blank.

The code below doesn't work:

class User
  key :confirmation_token, String, :unique => true, :allow_nil => true, :allow_blank => true
end

# I have one user with confirmation_token = nil in DB
user = User.new
user.valid? # => false
user.errors # => @messages={:confirmation_token=>["Already exists!"]

So, it validates for uniqueness anyway

Owner

cheald commented Jul 5, 2013

I don't think that this is actually a bug - MongoDB doesn't allow multiple nil or blank fields in a unique index, whereas ActiveRecord's target DBs do.

> db.uniques.insert({name: null})
> db.uniques.ensureIndex({name: 1}, {unique: 1})
> db.uniques.insert({name: null})
E11000 duplicate key error index: test.uniques.$name_1  dup key: { : null }

Allowing AR-style behavior would lead to actual index conflicts when you have a unique index defined on the collection.

@cheald cheald closed this Jul 5, 2013

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