Module: ActiveModel::Validations::HelperMethods

Defined in:
lib/active_model/validations/association.rb

Instance Method Summary collapse

Instance Method Details

#validates_association_of(*attr_names) ⇒ Object

Validates that the specified attributes and associations are not blank(as defined by Object#blank?) . Happens by default on save. Example:

class Patient < ActiveRecord::Base
  belongs_to :person
  validates_association_of :person_id
end

The person_id attribute and the person association must be the object and can not be blank unless presence is false.

Configuration options:

  • :presence - The verification of presence of the attribute (default is: true).

  • :association - The association name (default is: nil).

  • :blank_message - A custom error message (default is: “can’t be blank”).

  • :invalid_message - A custom error message (default is: “is invalid”).

  • :not_relationship_message - A custom error message (default is: “is not a relationship”).

  • :on - Specifies when this validation is active. Runs in all validation contexts by default (nil), other options are :create and :update.

  • :if - Specifies a method, proc or string to call to determine if the validation should occur (e.g. :if => :allow_validation, or :if => Proc.new { |user| user.signup_step > 2 }). The method, proc or string should return or evaluate to a true or false value.

  • :unless - Specifies a method, proc or string to call to determine if the validation should not occur (e.g. :unless => :skip_validation, or :unless => Proc.new { |user| user.signup_step <= 2 }). The method, proc or string should return or evaluate to a true or false value.

  • :strict - Specifies whether validation should be strict. See ActiveModel::Validation#validates! for more information.



60
61
62
# File 'lib/active_model/validations/association.rb', line 60

def validates_association_of(*attr_names)
  validates_with AssociationValidator, _merge_attributes(attr_names)
end