Active Records implement validation by overwriting Base#validate (or the variations, validate_on_create and 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.
Please do have a look at ActiveRecord::Validations::ClassMethods for a higher level of validations.
- errors
- save_with_validation
- save_with_validation!
- update_attribute_with_validation_skipping
- valid?
- validate
- validate_on_create
- validate_on_update
| VALIDATIONS | = | %w( validate validate_on_create validate_on_update ) |
Returns the Errors object that holds all information about attribute error messages.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 795
795: def errors
796: @errors ||= Errors.new(self)
797: end
The validation process on save can be skipped by passing false. The regular Base#save method is replaced with this when the validations module is mixed in, which it is by default.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 750
750: def save_with_validation(perform_validation = true)
751: if perform_validation && valid? || !perform_validation
752: save_without_validation
753: else
754: false
755: end
756: end
Attempts to save the record just like Base#save but will raise a RecordInvalid exception instead of returning false if the record is not valid.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 760
760: def save_with_validation!
761: if valid?
762: save_without_validation!
763: else
764: raise RecordInvalid.new(self)
765: end
766: end
Updates a single attribute and saves the record without going through the normal validation procedure. This is especially useful for boolean flags on existing records. The regular update_attribute method in Base is replaced with this when the validations module is mixed in, which it is by default.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 771
771: def update_attribute_with_validation_skipping(name, value)
772: send(name.to_s + '=', value)
773: save(false)
774: end
Runs validate and validate_on_create or validate_on_update and returns true if no errors were added otherwise false.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 777
777: def valid?
778: errors.clear
779:
780: run_validations(:validate)
781: validate
782:
783: if new_record?
784: run_validations(:validate_on_create)
785: validate_on_create
786: else
787: run_validations(:validate_on_update)
788: validate_on_update
789: end
790:
791: errors.empty?
792: end
Overwrite this method for validation checks on all saves and use Errors.add(field, msg) for invalid attributes.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 801
801: def validate #:doc:
802: end
Overwrite this method for validation checks used only on creation.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 805
805: def validate_on_create #:doc:
806: end
Overwrite this method for validation checks used only on updates.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/validations.rb, line 809
809: def validate_on_update # :doc:
810: end