module ActiveRecord::Validations
def custom_validation_context? # :nodoc:
def custom_validation_context? # :nodoc: validation_context && [:create, :update].exclude?(validation_context) end
def default_validation_context
def default_validation_context new_record? ? :create : :update end
def perform_validations(options = {})
def perform_validations(options = {}) options[:validate] == false || valid?(options[:context]) end
def raise_validation_error
def raise_validation_error raise(RecordInvalid.new(self)) end
def save(**options)
The regular {ActiveRecord::Base#save}[rdoc-ref:Persistence#save] method is replaced
The validation context can be changed by passing context: context.
The validation process on save can be skipped by passing validate: false.
def save(**options) perform_validations(options) ? super : false end
def save!(**options)
Attempts to save the record just like {ActiveRecord::Base#save}[rdoc-ref:Base#save] but
def save!(**options) perform_validations(options) ? super : raise_validation_error end
def valid?(context = nil)
\Validations with no :on option will run no matter the context. \Validations with
run within multiple contexts.
If the argument is an array of contexts, post.valid?([:create, :update]), the validations are
{new_record?}[rdoc-ref:Persistence#new_record?] is +true+, and to :update if it is not.
If the argument is +false+ (default is +nil+), the context is set to :create if
Aliased as #validate.
no errors are found, +false+ otherwise.
Runs all the validations within the specified context. Returns +true+ if
def valid?(context = nil) context ||= default_validation_context output = super(context) errors.empty? && output end