Class: Mongoid::Validatable::UniquenessValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/mongoid/core_ext/validatable/uniqueness.rb

Overview

Validates whether or not a field is unique against the documents in the database.

Examples:

Define the uniqueness validator.


class Person
  include Mongoid::Document
  field :title

  validates_uniqueness_of :title
end

Instance Method Summary collapse

Instance Method Details

#scope(criteria, document, attribute) ⇒ Criteria

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Scope the criteria to the scope options provided.

Examples:

Scope the criteria.

validator.scope(criteria, document)

Parameters:

  • criteria (Criteria)

    The criteria to scope.

  • document (Document)

    The document being validated.

Returns:

  • (Criteria)

    The scoped criteria.

Since:

  • 2.3.0



31
32
33
34
35
36
37
38
# File 'lib/mongoid/core_ext/validatable/uniqueness.rb', line 31

def scope(criteria, document, attribute)
  Array.wrap(options[:scope]).each do |item|
    name = document.database_field_name(item)
    criteria = criteria.where(item => document.attributes[name])
  end
  criteria = criteria.where(deleted_at: nil) if document.paranoid?
  criteria
end