Class: Mongoid::Validatable::AssociatedValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
lib/mongoid/validatable/associated.rb

Overview

Validates whether or not an association is valid or not. Will correctly handle has one and has many associations.

Examples:

Set up the association validations.


class Person
  include Mongoid::Document
  embeds_one :name
  embeds_many :addresses

  validates_associated :name, :addresses
end

Instance Method Summary collapse

Instance Method Details

#attributesObject

Required by ‘validates_with` so that the validator gets added to the correct attributes.



22
23
24
# File 'lib/mongoid/validatable/associated.rb', line 22

def attributes
  options[:attributes]
end

#validate(document) ⇒ Object

Checks that the named associations of the given record (‘attributes`) are valid. This does NOT load the associations from the database, and will only validate records that are dirty or unpersisted.

If anything is not valid, appropriate errors will be added to the ‘document` parameter.

Parameters:



36
37
38
39
40
# File 'lib/mongoid/validatable/associated.rb', line 36

def validate(document)
  options[:attributes].each do |attr_name|
    validate_association(document, attr_name)
  end
end