Allow blank on should validate_uniqueness_of

Hello,

Using shoulda, any ideas how to allow blank when having this test:

should validate_uniqueness_of(:email)

Thanks.

:allow_blank => true

Where do you pass that?

should validate_uniqueness_of(:email, :allow_blank => true)

That doesn't work. That method takes only one argument:

/Users/pupeno/.rvm/gems/ruby-1.8.7-p174@lemonfrog/gems/shoulda-2.11.0/lib/shoulda/context.rb:429:in
`validate_uniqueness_of': wrong number of arguments (2 for 1)
(ArgumentError)
        from /Users/pupeno/.rvm/gems/ruby-1.8.7-p174@lemonfrog/gems/shoulda-2.11.0/lib/shoulda/context.rb:429:in
`send'
        from /Users/pupeno/.rvm/gems/ruby-1.8.7-p174@lemonfrog/gems/shoulda-2.11.0/lib/shoulda/context.rb:429:in
`method_missing'
        from ./test/unit/meta_data_test.rb:14:in `__bind_1276752425_894930'
        from /Users/pupeno/.rvm/gems/ruby-1.8.7-p174@lemonfrog/gems/shoulda-2.11.0/lib/shoulda/context.rb:306:in
`call'
        from /Users/pupeno/.rvm/gems/ruby-1.8.7-p174@lemonfrog/gems/shoulda-2.11.0/lib/shoulda/context.rb:306:in
`merge_block'
        from /Users/pupeno/.rvm/gems/ruby-1.8.7-p174@lemonfrog/gems/shoulda-2.11.0/lib/shoulda/context.rb:301:in
`initialize'
        from /Users/pupeno/.rvm/gems/ruby-1.8.7-p174@lemonfrog/gems/shoulda-2.11.0/lib/shoulda/context.rb:199:in
`new'
        from /Users/pupeno/.rvm/gems/ruby-1.8.7-p174@lemonfrog/gems/shoulda-2.11.0/lib/shoulda/context.rb:199:in
`context'
        from ./test/unit/meta_data_test.rb:8
        from /Users/pupeno/.rvm/gems/ruby-1.8.7-p174@lemonfrog/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:147:in
`load_without_new_constant_marking'
        from /Users/pupeno/.rvm/gems/ruby-1.8.7-p174@lemonfrog/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:147:in
`load'
        from /Users/pupeno/.rvm/gems/ruby-1.8.7-p174@lemonfrog/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5
        from /Users/pupeno/.rvm/gems/ruby-1.8.7-p174@lemonfrog/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5:in
`each'
        from /Users/pupeno/.rvm/gems/ruby-1.8.7-p174@lemonfrog/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5

Where do you pass that?

should validate_uniqueness_of(:email, :allow_blank => true)

That doesn't work. That method takes only one argument:

group/rubyonrails-talk?hl=en.

--
J. Pablo Fernández <pup...@pupeno.com> (http://pupeno.com)

Here is a link to the validation method doc:

http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M001400

You should be able to use :allow_blank => true, unless your version of
Rails does not support the option, which I guess could be possible? If
that is the case you should be able to use :if or :unless:

Supposing your model is for a customer:

# The 'c' inside the proc represents the customer record you're
validating (you can, of course, use any variable name you want).
validate_uniqueness_of :email, :unless => Proc.new {|c|
c.email.blank?}

You have to take into consideration that if your DB table has a unique
constraint on the e-mail column you'll still run into problems because
the DB itself will not let you add 2 records with an empty e-mail
address, only one. The second attempt will fail.

Hope it helps.

I think there is confusion about your question. Are you asking how to
allow blank in the validation itself (in which case allows_blank in
the call of validates_uniqueness of is the way to go) or are you
asking how to code the test to check that multiple blank emails are
allowed?

Colin

Is the solution to allow null for the email rather than empty string?
Will the unique constraint fail in that case?

Colin

I'm asking how to test the validation with shoulda with the new
syntax, hence the line:

should validate_uniqueness_of(:email)

pepe, I'm not asking how to do the validation, I'm asking how to test
the validation with shoulda using the new syntax, I already have this
line in the model:

validate_uniqueness_of :email, :allow_blank => true

in my tests I have:

should validate_uniqueness_of(:email)

should being a shoulda method.

No, allowing blank is fine, the validation is this:

validate_uniqueness_of(:email, :allow_blank => true)

The issue is writing the test.