Rails 3: Validation Reflection

- -

A recent addition to ActiveModel is the introduction of a couple of methods for validation reflection.

Say we have a sample Person class which includes the ActiveModel::Validations module so we can validate a couple of attributes:

require 'active_model'

class Person
  include ActiveModel::Validations
  attr_accessor :name, :age
  validates :name, :presence => true
  validates :age,  :presence => true, :numericality => true
end

New to ActiveModel::Validations is the class method validators, which returns an array of Validator objects associated with the model:

Person.validators
#> [#<ActiveModel::Validations::PresenceValidator:0x101565cc8 @attributes=[:age], @options={}>,
#   #<ActiveModel::Validations::NumericalityValidator:0x101562640 @attributes=[:age],
#       @options={:only_integer=>false, :allow_nil=>false}>,
#   #<ActiveModel::Validations::PresenceValidator:0x1015691c0 @attributes=[:name], @options={}>]

Another addition is the class method validators_on, which provides a convenient way of accessing the validators that are associated with a particular attribute on the model:

Person.validators_on(:name)
#> [#<ActiveModel::Validations::PresenceValidator:0x1015691c0 @attributes=[:name], @options={}>]

Person.validators_on(:age)
#> [#<ActiveModel::Validations::PresenceValidator:0x101565cc8 @attributes=[:age], @options={}>,
#   #<ActiveModel::Validations::NumericalityValidator:0x101562640 @attributes=[:age],
#       @options={:only_integer=>false, :allow_nil=>false}>]

Notes

We're passionate about understanding businesses, ideas and people. Let's Talk