Understanding Rails validation: what does allow_blank do?
The following distinction can be useful to know:
presence: true # nil and empty string fail validation
presence: true, allow_blank: true # nil fails validation, empty string passes
What you've got is equivalent to this (wrapped for clarity):
validates :email, :presence => true,
:uniqueness => { :allow_blank => true, :case_sensitive => false }
That's a little silly though since if you're requiring presence, then that's going to "invalidate" the :allow_blank
clause to :uniqueness
.
It makes more sense when you switch to using other validators.. say... format
and uniqueness
, but you don't want any checks if it's blank. In this case, adding a "globally applied" :allow_blank
makes more sense and DRY's up the code a little bit.
This...
validates :email, :format => {:allow_blank => true, ...},
:uniqueness => {:allow_blank => true, ...}
can be written like:
validates :email, :allow_blank => true, :format => {...}, :uniqueness => {...}