Module: CouchRest::Validation::ValidatesUniqueness

Included in:
ClassMethods
Defined in:
lib/couchrest-uniqueness-validation.rb

Overview

class UniquenessValidator

Instance Method Summary collapse

Instance Method Details

#validates_uniqueness_of(*fields) ⇒ Object

Validates that the specified attribute is unique across documents with the same couchrest-type using a view.

Example

require 'rubygems'
require 'couchrest'
require 'couchrest-uniqueness-validation' 

class User < CouchRest::ExtendedDocument

  property :nickname
  property :login

  view_by :nickname

  # assumes that a view :by_nickname is present
  validates_uniqueness_of :nickname

  # uses a different from default view 
  validates_uniqueness_of :login, :view => 'my_custom_view'

  # a call to valid? will return false unless no other document exists with
  # the same couchrest-type, nickname and login
end

Note: at least two views should exist in the User design doc for this example to work - :by_nickname and :my_custom_view.

See CouchRest Views docs for more info on views



73
74
75
76
# File 'lib/couchrest-uniqueness-validation.rb', line 73

def validates_uniqueness_of(*fields)
  opts = opts_from_validator_args(fields)
  add_validator_to_context(opts, fields, CouchRest::Validation::UniquenessValidator)
end