Sha256: f1a50a6581479ff1443701f0eb363a379f2556c74218e7327d19cee01725abfc

Contents?: true

Size: 657 Bytes

Versions: 2

Compression:

Stored size: 657 Bytes

Contents

# encoding: utf-8
module Mongoid #:nodoc:
  module Validations #:nodoc:
    # Validates whether or not a field is unique against the documents in the
    # database.
    #
    # Example:
    #
    #   class Person
    #     include Mongoid::Document
    #     field :title
    #
    #     validates_uniqueness_of :title
    #   end
    class UniquenessValidator < ActiveModel::EachValidator
      def validate_each(document, attribute, value)
        return if document.class.where(attribute => value, :_id.ne => document._id).empty?
        document.errors.add(attribute, :taken, :default => options[:message], :value => value)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
mongoid-pre-2.0.0.beta1 lib/mongoid/validations/uniqueness.rb
mongoid-2.0.0.alpha lib/mongoid/validations/uniqueness.rb