validates_uniqueness_of passes on nil or blank (without allow_nil and allow_blank)

You are mistaken about the default behavior. From the docs:

:allow_nil – If set to true, skips this validation if the attribute is nil (default is false).
:allow_blank – If set to true, skips this validation if the attribute is blank (default is false, it includes nil too).

Setting allow_blank to true, I see the following behavior with Rails 2.3.4.

class Thing < ActiveRecord::Base
  validates_uniqueness_of :identification, :allow_blank => true
end

>> Thing.create! :identification => ""
=> #<Thing id: 6, identification: "", created_at: "2009-09-26 03:09:48", updated_at: "2009-09-26 03:09:48">
>> Thing.create! :identification => ""
=> #<Thing id: 7, identification: "", created_at: "2009-09-26 03:09:49", updated_at: "2009-09-26 03:09:49">
>> Thing.create! :identification => nil
=> #<Thing id: 8, identification: nil, created_at: "2009-09-26 03:09:52", updated_at: "2009-09-26 03:09:52">
>> Thing.create! :identification => nil
=> #<Thing id: 9, identification: nil, created_at: "2009-09-26 03:09:53", updated_at: "2009-09-26 03:09:53">

Edit: Addressing your clarification.

Adding a validates_presence_of would be correct for what you’re trying to do. It’s not redundant, since it’s checking for a completely different error case. It also has its own error message, which will be important for the user.

class Thing < ActiveRecord::Base
  validates_uniqueness_of :identification, :allow_blank => true
  validates_presence_of :identification
end

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)