instance method on

Ruby on Rails 2.3.18

Since v2.2.3 Last seen in v2.3.18

Available in: v2.2.3 v2.3.18

Signature

on(attribute)

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

Parameters

attribute req
Source
# File activerecord/lib/active_record/validations.rb, line 212
    def on(attribute)
      attribute = attribute.to_s
      return nil unless @errors.has_key?(attribute)
      errors = @errors[attribute].map(&:to_s)
      errors.size == 1 ? errors.first : errors
    end

Defined in activerecord/lib/active_record/validations.rb line 212 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveRecord::Errors

Type at least 2 characters to search.

↑↓ navigate · open · esc close