Module: ActiveRecord::Validations

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::Validations
Defined in:
activerecord/lib/active_record/validations.rb,
activerecord/lib/active_record/validations/associated.rb,
activerecord/lib/active_record/validations/uniqueness.rb

Defined Under Namespace

Modules: ClassMethods Classes: AssociatedValidator, UniquenessValidator

Instance Method Summary collapse

Methods included from ActiveSupport::Concern

append_features, extended, included

Methods included from ActiveModel::Validations

#errors, #invalid?, #validates_with

Methods included from ActiveSupport::Callbacks

#callback, #run_callbacks

Instance Method Details

#save(options = {}) ⇒ Object

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.



42
43
44
# File 'activerecord/lib/active_record/validations.rb', line 42

def save(options={})
  perform_validations(options) ? super : false
end

#save!(options = {}) ⇒ Object

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.



48
49
50
# File 'activerecord/lib/active_record/validations.rb', line 48

def save!(options={})
  perform_validations(options) ? super : raise(RecordInvalid.new(self))
end

#valid?(context = nil) ⇒ Boolean

Runs all the specified validations and returns true if no errors were added otherwise false.



53
54
55
56
57
58
59
60
61
# File 'activerecord/lib/active_record/validations.rb', line 53

def valid?(context = nil)
  context ||= (new_record? ? :create : :update)
  output = super(context)

  deprecated_callback_method(:validate)
  deprecated_callback_method(:validate_on_#{context}")

  errors.empty? && output
end