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.

J. Pablo Fernández [email protected] (http://pupeno.com)

2010/6/15 J. Pablo Fernández [email protected]:

Hello,

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

should validate_uniqueness_of(:email)

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

: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

On Tue, Jun 15, 2010 at 22:46, chewmanfoo [email protected] wrote:


J. Pablo Fernández [email protected] (http://pupeno.com)


You received this message because you are subscribed to the Google G. “Ruby on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected].
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.


J. Pablo Fernández [email protected] (http://pupeno.com)

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

should validate_uniqueness_of(:email)

On Thu, Jun 17, 2010 at 10:45, Colin L. [email protected] wrote:

asking how to code the test to check that multiple blank emails are


J. Pablo Fernández [email protected] (http://pupeno.com)

On Jun 17, 1:28 am, J. Pablo Fernández [email protected] wrote:

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 [email protected] (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.

On 17 June 2010 09:18, pepe [email protected] wrote:

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.

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

Colin

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.

On Thu, Jun 17, 2010 at 10:18, pepe [email protected] wrote:

The ‘c’ inside the proc represents the customer record you’re


You received this message because you are subscribed to the Google G. “Ruby on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected].
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.


J. Pablo Fernández [email protected] (http://pupeno.com)

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

validate_uniqueness_of(:email, :allow_blank => true)

The issue is writing the test.