Class ActiveRecord::Errors
In: vendor/rails/activerecord/lib/active_record/validations.rb
Parent: Object

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

Methods

Public Instance methods

[](attribute)

Alias for on

Adds an error message (msg) 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 msg is supplied, "invalid" is assumed.

[Source]

    # File vendor/rails/activerecord/lib/active_record/validations.rb, line 43
43:     def add(attribute, msg = @@default_error_messages[:invalid])
44:       @errors[attribute.to_s] = [] if @errors[attribute.to_s].nil?
45:       @errors[attribute.to_s] << msg
46:     end

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

[Source]

    # File vendor/rails/activerecord/lib/active_record/validations.rb, line 58
58:     def add_on_blank(attributes, msg = @@default_error_messages[:blank])
59:       for attr in [attributes].flatten
60:         value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s]
61:         add(attr, msg) if value.blank?
62:       end
63:     end

Will add an error message to each of the attributes in attributes that has a length outside of the passed boundary range. If the length is above the boundary, the too_long_msg message will be used. If below, the too_short_msg.

[Source]

    # File vendor/rails/activerecord/lib/active_record/validations.rb, line 67
67:     def add_on_boundary_breaking(attributes, range, too_long_msg = @@default_error_messages[:too_long], too_short_msg = @@default_error_messages[:too_short])
68:       for attr in [attributes].flatten
69:         value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s]
70:         add(attr, too_short_msg % range.begin) if value && value.length < range.begin
71:         add(attr, too_long_msg % range.end) if value && value.length > range.end
72:       end
73:     end
add_on_boundry_breaking(attributes, range, too_long_msg = @@default_error_messages[:too_long], too_short_msg = @@default_error_messages[:too_short])

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

[Source]

    # File vendor/rails/activerecord/lib/active_record/validations.rb, line 49
49:     def add_on_empty(attributes, msg = @@default_error_messages[:empty])
50:       for attr in [attributes].flatten
51:         value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s]
52:         is_empty = value.respond_to?("empty?") ? value.empty? : false
53:         add(attr, msg) unless !value.nil? && !is_empty
54:       end
55:     end

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

[Source]

    # File vendor/rails/activerecord/lib/active_record/validations.rb, line 35
35:     def add_to_base(msg)
36:       add(:base, msg)
37:     end

Removes all the errors that have been added.

[Source]

     # File vendor/rails/activerecord/lib/active_record/validations.rb, line 138
138:     def clear
139:       @errors = {}
140:     end

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

[Source]

     # File vendor/rails/activerecord/lib/active_record/validations.rb, line 144
144:     def count
145:       error_count = 0
146:       @errors.each_value { |attribute| error_count += attribute.length }
147:       error_count
148:     end

Yields each attribute and associated message per error added.

[Source]

     # File vendor/rails/activerecord/lib/active_record/validations.rb, line 103
103:     def each
104:       @errors.each_key { |attr| @errors[attr].each { |msg| yield attr, msg } }
105:     end

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".

[Source]

     # File vendor/rails/activerecord/lib/active_record/validations.rb, line 109
109:     def each_full
110:       full_messages.each { |msg| yield msg }
111:     end

Returns true if no errors have been added.

[Source]

     # File vendor/rails/activerecord/lib/active_record/validations.rb, line 133
133:     def empty?
134:       return @errors.empty?
135:     end

Returns all the full error messages in an array.

[Source]

     # File vendor/rails/activerecord/lib/active_record/validations.rb, line 114
114:     def full_messages
115:       full_messages = []
116:       
117:       @errors.each_key do |attr| 
118:         @errors[attr].each do |msg|
119:           next if msg.nil?
120:           
121:           if attr == "base"
122:             full_messages << msg
123:           else
124:             full_messages << @base.class.human_attribute_name(attr) + " " + msg
125:           end
126:         end
127:       end
128:       
129:       return full_messages
130:     end

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

[Source]

    # File vendor/rails/activerecord/lib/active_record/validations.rb, line 78
78:     def invalid?(attribute)
79:       !@errors[attribute.to_s].nil?
80:     end
  • 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.

[Source]

    # File vendor/rails/activerecord/lib/active_record/validations.rb, line 85
85:     def on(attribute)
86:       if @errors[attribute.to_s].nil?
87:         nil
88:       elsif @errors[attribute.to_s].length == 1
89:         @errors[attribute.to_s].first
90:       else
91:         @errors[attribute.to_s]
92:       end
93:     end

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

[Source]

     # File vendor/rails/activerecord/lib/active_record/validations.rb, line 98
 98:     def on_base
 99:       on(:base)
100:     end

[Validate]