Class

ActiveRecord::Errors

Inheritance
< Object
Included Modules
Enumerable

Active Record validation is reported to and from this object, which is used by Base#save to determine whether the object is in a valid state to be saved. See usage example in Validations.

Methods

Class

Visibility Signature
public default_error_messages ()

Instance

Visibility Signature
public [] (attribute)
public add (attribute, message = nil, options = {})
public add_on_blank (attributes, custom_message = nil)
public add_on_empty (attributes, custom_message = nil)
public add_to_base (msg)
public clear ()
public count ()
public each () {|attr, msg| ...}
public each_full () {|msg| ...}
public empty? ()
public full_messages (options = {})
public generate_message (attribute, message = :invalid, options = {})
public invalid? (attribute)
public length ()
public on (attribute)
public on_base ()
public size ()
public to_xml (options={})

Class Method Detail

default_error_messages()

Instance Method Detail

[](attribute)

Alias for on

add(attribute, message = nil, options = {})

Adds an error message (messsage) to the attribute, which will be returned on a call to on(attribute) for the same attribute and ensure that this error object returns false when asked if empty?. More than one error can be added to the same attribute in which case an array will be returned on a call to on(attribute). If no messsage is supplied, :invalid is assumed. If message is a Symbol, it will be translated, using the appropriate scope (see translate_error).

add_on_blank(attributes, custom_message = nil)

Will add an error message to each of the attributes in attributes that is blank (using Object#blank?).

add_on_empty(attributes, custom_message = nil)

Will add an error message to each of the attributes in attributes that is empty.

add_to_base(msg)

Adds an error to the base object instead of any particular attribute. This is used to report errors that don‘t tie to any specific attribute, but rather to the object as a whole. These error messages don‘t get prepended with any field name when iterating with each_full, so they should be complete sentences.

clear()

Removes all errors that have been added.

count()

Alias for size

each() {|attr, msg| ...}

Yields each attribute and associated message per error added.

  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.each{|attr,msg| puts "#{attr} - #{msg}" }
  # => name - is too short (minimum is 5 characters)
  #    name - can't be blank
  #    address - can't be blank

each_full() {|msg| ...}

Yields each full error message added. So Person.errors.add("first_name", "can‘t be empty") will be returned through iteration as "First name can‘t be empty".

  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.each_full{|msg| puts msg }
  # => Name is too short (minimum is 5 characters)
  #    Name can't be blank
  #    Address can't be blank

empty?()

Returns true if no errors have been added.

full_messages(options = {})

Returns all the full error messages in an array.

  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.full_messages # =>
    ["Name is too short (minimum is 5 characters)", "Name can't be blank", "Address can't be blank"]

generate_message(attribute, message = :invalid, options = {})

Translates an error message in it‘s default scope (activerecord.errrors.messages). Error messages are first looked up in models.MODEL.attributes.ATTRIBUTE.MESSAGE, if it‘s not there, it‘s looked up in models.MODEL.MESSAGE and if that is not there it returns the translation of the default message (e.g. activerecord.errors.messages.MESSAGE). The translated model name, translated attribute name and the value are available for interpolation.

When using inheritence in your models, it will check all the inherited models too, but only if the model itself hasn‘t been found. Say you have class Admin < User; end and you wanted the translation for the :blank error message for the title attribute, it looks for these translations:

<ol> <li>activerecord.errors.models.admin.attributes.title.blank</li> <li>activerecord.errors.models.admin.blank</li> <li>activerecord.errors.models.user.attributes.title.blank</li> <li>activerecord.errors.models.user.blank</li> <li>activerecord.errors.messages.blank</li> <li>any default you provided through the options hash (in the activerecord.errors scope)</li> </ol>

invalid?(attribute)

Returns true if the specified attribute has errors associated with it.

  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.invalid?(:name)      # => true
  company.errors.invalid?(:address)   # => false

length()

Alias for size

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

on_base()

Returns errors assigned to the base object through add_to_base according to the normal rules of on(attribute).

size()

Returns the total number of errors added. Two errors added to the same attribute will be counted as such.

to_xml(options={})

Returns an XML representation of this error object.

  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.to_xml
  # =>  <?xml version="1.0" encoding="UTF-8"?>
  #     <errors>
  #       <error>Name is too short (minimum is 5 characters)</error>
  #       <error>Name can't be blank</error>
  #       <error>Address can't be blank</error>
  #     </errors>