You are here: Home Solidarrow Ruby on Rails Solidarrow Models

* ActiveRecord Validations Cheatsheet

Comprehensive but concise details of Rails' built-in class validation helper methods from validates_acceptance_of through to validates_uniqueness_of, along with details of how to roll your own low-level validations along with examples...

Common options

:message

a custom error message

:on

specifies when this validation is active (default is :save, other options :create, :update)

:allow_nil

skip validation if attribute is nil (default is false). Notice that for fixnum and float columns empty strings are converted to nil

:allow_blank

if set to true, skips this validation if the attribute is blank (default is false)

:if

specifies a method, proc or "string" to call to determine if the validation should occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The method, proc or "string" should return or evaluate to a true or false value.

:unless

specifies a method, proc or "string" to call to determine if the validation should not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }). The method, proc or "string" should return or evaluate to a true or false value.

 

 

> RELATED ARTICLE

* Active Record Validation Errors Cheatsheet

Each ActiveRecord backed model in Rails has an errors object which errors are added to if the model fails validation. This cheatsheet covers methods available, default errors messages and view error helpers. > More