Class: Mongoid::Validations::AssociatedValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- Mongoid::Validations::AssociatedValidator
- Defined in:
- lib/mongoid/validations/associated.rb
Overview
Validates whether or not an association is valid or not. Will correctly handle has one and has many associations.
Instance Method Summary collapse
-
#validate_each(document, attribute, value) ⇒ Object
Validates that the associations provided are either all nil or all valid.
Instance Method Details
#validate_each(document, attribute, value) ⇒ Object
Validates that the associations provided are either all nil or all valid. If neither is true then the appropriate errors will be added to the parent document.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/mongoid/validations/associated.rb', line 29 def validate_each(document, attribute, value) begin document.begin_validate valid = Array.wrap(value).collect do |doc| if doc.nil? true else doc.validated? ? true : doc.valid? end end.all? ensure document.exit_validate end document.errors.add(attribute, :invalid) unless valid end |