Parent

Files

Class Index [+]

Quicksearch

ActiveRecord::Validations::UniquenessValidator

Public Class Methods

new(options) click to toggle source
   # File lib/active_record/validations/uniqueness.rb, line 6
6:       def initialize(options)
7:         super(options.reverse_merge(:case_sensitive => true))
8:       end

Public Instance Methods

setup(klass) click to toggle source

Unfortunately, we have to tie Uniqueness validators to a class.

    # File lib/active_record/validations/uniqueness.rb, line 11
11:       def setup(klass)
12:         @klass = klass
13:       end
validate_each(record, attribute, value) click to toggle source
    # File lib/active_record/validations/uniqueness.rb, line 15
15:       def validate_each(record, attribute, value)
16:         finder_class = find_finder_class_for(record)
17:         table = finder_class.arel_table
18: 
19:         coder = record.class.serialized_attributes[attribute.to_s]
20: 
21:         if value && coder
22:           value = coder.dump value
23:         end
24: 
25:         relation = build_relation(finder_class, table, attribute, value)
26:         relation = relation.and(table[finder_class.primary_key.to_sym].not_eq(record.send(:id))) if record.persisted?
27: 
28:         Array.wrap(options[:scope]).each do |scope_item|
29:           scope_value = record.send(scope_item)
30:           relation = relation.and(table[scope_item].eq(scope_value))
31:         end
32: 
33:         if finder_class.unscoped.where(relation).exists?
34:           record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope).merge(:value => value))
35:         end
36:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.