module ActiveRecord::Validations::ClassMethods
def validates_associated(*attr_names)
method, proc, or string should return or evaluate to a +true+ or +false+
or unless: Proc.new { |user| user.signup_step <= 2 }). The
determine if the validation should not occur (e.g. unless: :skip_validation,
* :unless - Specifies a method, proc, or string to call to
proc or string should return or evaluate to a +true+ or +false+ value.
or if: Proc.new { |user| user.signup_step > 2 }). The method,
if the validation should occur (e.g. if: :allow_validation,
* :if - Specifies a method, proc, or string to call to determine
on: [:create, :custom_validation_context])
on: :custom_validation_context or
or an array of symbols. (e.g. on: :create or
Runs in all validation contexts by default +nil+. You can pass a symbol
* :on - Specifies the contexts where this validation is active.
* :message - A custom error message (default is: "is invalid").
Configuration options:
{validates_presence_of}[rdoc-ref:Validations::ClassMethods#validates_presence_of].
guaranteed to be valid, you also need to use
assigned. If you want to ensure that the association is both present and
NOTE: This validation will not fail if the association hasn't been
Doing so will lead to a circular dependency and cause infinite recursion.
WARNING: This validation must not be used on both ends of an association.
end
validates_associated :pages, :library
belongs_to :library
has_many :pages
class Book < ActiveRecord::Base
Works with any kind of association.
Validates whether the associated object or objects are all valid.
def validates_associated(*attr_names) validates_with AssociatedValidator, _merge_attributes(attr_names) end