method

on

Importance_2
v2.1.0 - Show latest stable - 1 note - Class: ActiveRecord::Errors
on(attribute) public

Returns nil, if no errors are associated with the specified attribute. Returns the error message, if one error is associated with the specified attribute. Returns an array of error messages, if more than one error is associated with the specified attribute.

  class Company < ActiveRecord::Base
    validates_presence_of :name, :address, :email
    validates_length_of :name, :in => 5..30
  end

  company = Company.create(:address => '123 First St.')
  company.errors.on(:name)      # => ["is too short (minimum is 5 characters)", "can't be blank"]
  company.errors.on(:email)     # => "can't be blank"
  company.errors.on(:address)   # => nil
Show source
Register or log in to add new notes.
August 8, 2014 - (v3.0.0 - v4.0.2)
0 thanks

Deprecated in favour of []

In Rails 3+

company.errors[:name]

In Rails 2.3 and below:

company.errors.on(:name)