Please do have a look at ActiveRecord::Validations::ClassMethods for a higher level of validations.

Active Records implement validation by overwriting Base#validate (or the variations, validate_on_createand validate_on_update). Each of these methods can inspect the state of the object, which usually means ensuring that a number of attributes have a certain value (such as not empty, within a given range, matching a certain regular expression).

Example:

class Person < ActiveRecord::Base
  protected
    def validate
      errors.add_on_empty %w( first_name last_name )
      errors.add("phone_number", "has invalid format") unless phone_number =~ /[0-9]*/
    end
    def validate_on_create # is only run the first time a new object is saved
      unless valid_discount?(membership_discount)
        errors.add("membership_discount", "has expired")
      end
    end
    def validate_on_update
      errors.add_to_base("No changes have occurred") if unchanged_attributes?
    end
end
person = Person.new("first_name" => "David", "phone_number" => "what?")
person.save                         # => false (and doesn't do the save)
person.errors.empty?                # => false
person.errors.count                 # => 2
person.errors.on "last_name"        # => "can't be empty"
person.errors.on "phone_number"     # => "has invalid format"
person.errors.each_full { |msg| puts msg }
                                    # => "Last name can't be empty\n" +
                                    #    "Phone number has invalid format"
person.attributes = { "last_name" => "Heinemeier", "phone_number" => "555-555" }
person.save # => true (and person is now saved in the database)

An Errors object is automatically created for every Active Record.

Namespace
Methods
E
I
S
V
Constants
VALIDATIONS = %w( validate validate_on_create validate_on_update )
 
Instance Public methods
errors()

Returns the Errors object that holds all information about attribute error messages.

# File activerecord/lib/active_record/validations.rb, line 1129
def errors
  @errors ||= Errors.new(self)
end
invalid?()

Performs the opposite of valid?. Returns true if errors were added, false otherwise.

# File activerecord/lib/active_record/validations.rb, line 1124
def invalid?
  !valid?
end
save_with_validation(perform_validation = true)

The validation process on save can be skipped by passing false. The regular ActiveRecord::Base#save method is replaced with this when the validations module is mixed in, which it is by default.

# File activerecord/lib/active_record/validations.rb, line 1087
def save_with_validation(perform_validation = true)
  if perform_validation && valid? || !perform_validation
    save_without_validation
  else
    false
  end
end
save_with_validation!()

Attempts to save the record just like ActiveRecord::Base#save but will raise a RecordInvalid exception instead of returning false if the record is not valid.

# File activerecord/lib/active_record/validations.rb, line 1097
def save_with_validation!
  if valid?
    save_without_validation!
  else
    raise RecordInvalid.new(self)
  end
end
valid?()

Runs validateand validate_on_createor validate_on_updateand returns true if no errors were added otherwise false.

# File activerecord/lib/active_record/validations.rb, line 1106
def valid?
  errors.clear
  run_callbacks(:validate)
  validate
  if new_record?
    run_callbacks(:validate_on_create)
    validate_on_create
  else
    run_callbacks(:validate_on_update)
    validate_on_update
  end
  errors.empty?
end
Instance Protected methods
validate()

Overwrite this method for validation checks on all saves and use Errors.add(field, msg)for invalid attributes.

# File activerecord/lib/active_record/validations.rb, line 1135
def validate
end
validate_on_create()

Overwrite this method for validation checks used only on creation.

# File activerecord/lib/active_record/validations.rb, line 1139
def validate_on_create
end
validate_on_update()

Overwrite this method for validation checks used only on updates.

# File activerecord/lib/active_record/validations.rb, line 1143
def validate_on_update
end