Class: ActiveModel::Validations::UniquenessValidator

Inherits:
EachValidator
  • Object
show all
Defined in:
lib/active_mongo_uniquenesss.rb

Instance Method Summary collapse

Instance Method Details

#validate(record) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/active_mongo_uniquenesss.rb', line 6

def validate(record)
  attribute = attributes[0]
  
  query = {}
  query[:_id] = {"$ne" => record._id}
  query[attribute] = record.get_var(attribute) 
  
  options[:scope].each do |scope|
    query[scope] = record.get_var(scope)
  end
  
  int = record.class.count( query )
  
  record.errors.add(attribute, options[:message]) if int > 0
end