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).
Setting both of those to true, I see the following behavior with Rails 2.3.4.
class Thing < ActiveRecord::Base
validates_uniqueness_of :identification, :allow_blank => true, :allow_nil => 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_nil => true, :allow_blank => true
validates_presence_of :identification
end
and with validates
:
validates :email, uniqueness: { allow_blank: true }
# or
validates :email, uniqueness: { allow_nil: true }