Class: Mongoid::Validatable::UniquenessValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- Mongoid::Validatable::UniquenessValidator
- Includes:
- Queryable
- Defined in:
- lib/mongoid/validatable/uniqueness.rb
Overview
Validates whether or not a field is unique against the documents in the database.
It is also possible to limit the uniqueness constraint to a set of documents matching certain conditions:
class Person
include Mongoid::Document
field :title
field :active, type: Boolean
validates_uniqueness_of :title, conditions: -> {where(active: true)}
end
Instance Method Summary collapse
-
#validate_each(document, attribute, value) ⇒ Errors
Validate the document for uniqueness violations.
Methods included from Queryable
Instance Method Details
#validate_each(document, attribute, value) ⇒ Errors
Validate the document for uniqueness violations.
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/mongoid/validatable/uniqueness.rb', line 41 def validate_each(document, attribute, value) with_query(document) do attrib, val = to_validate(document, attribute, value) return unless validation_required?(document, attrib) if document. (document, attrib, val) else validate_root(document, attrib, val) end end end |