Method: NoBrainer::Document::Validation::Uniqueness::UniquenessValidator#validate_each

Defined in:
lib/no_brainer/document/validation/uniqueness.rb

#validate_each(doc, attr, value) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/no_brainer/document/validation/uniqueness.rb', line 73

def validate_each(doc, attr, value)
  criteria = self.model.unscoped.where(attr => value)
  criteria = apply_scopes(criteria, doc)
  criteria = exclude_doc(criteria, doc) if doc.persisted?
  doc.errors.add(attr, :taken, **options.except(:scope).merge(:value => value)) unless criteria.empty?
rescue NoBrainer::Error::InvalidType
  # We can't run the uniqueness validator: where() won't accept bad types
  # and we have some values that don't have the right type.
  # Note that it's fine to not add errors because the type validations will
  # prevent the document from being saved.
end