method
validate_each
v4.2.7 -
Show latest stable
- Class:
ActiveRecord::Validations::UniquenessValidator
validate_each(record, attribute, value)public
No documentation available.
# File activerecord/lib/active_record/validations/uniqueness.rb, line 13
def validate_each(record, attribute, value)
finder_class = find_finder_class_for(record)
table = finder_class.arel_table
value = map_enum_attribute(finder_class, attribute, value)
begin
relation = build_relation(finder_class, table, attribute, value)
if record.persisted?
if finder_class.primary_key
relation = relation.and(table[finder_class.primary_key.to_sym].not_eq(record.id))
else
raise UnknownPrimaryKey.new(finder_class, "Can not validate uniqueness for persisted record without primary key.")
end
end
relation = scope_relation(record, table, relation)
relation = finder_class.unscoped.where(relation)
relation = relation.merge(options[:conditions]) if options[:conditions]
rescue RangeError
relation = finder_class.none
end
if relation.exists?
error_options = options.except(:case_sensitive, :scope, :conditions)
error_options[:value] = value
record.errors.add(attribute, :taken, error_options)
end
end