Module: ActiveRecord::Validations

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

Defined Under Namespace

Modules: ClassMethods Classes: AssociatedValidator, UniquenessValidator

Instance Method Summary collapse

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

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
# File '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