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.unscoped
18: 
19:         table_name   = record.class.quoted_table_name
20:         sql, params  = mount_sql_and_params(finder_class, table_name, attribute, value)
21: 
22:         relation = table.where(sql, *params)
23: 
24:         Array.wrap(options[:scope]).each do |scope_item|
25:           scope_value = record.send(scope_item)
26:           relation = relation.where(scope_item => scope_value)
27:         end
28: 
29:         unless record.new_record?
30:           # TODO : This should be in Arel
31:           relation = relation.where("#{record.class.quoted_table_name}.#{record.class.primary_key} <> ?", record.send(:id))
32:         end
33: 
34:         if relation.exists?
35:           record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope).merge(:value => value))
36:         end
37:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.