diff options
Diffstat (limited to 'activerecord/lib/active_record/validations/uniqueness.rb')
-rw-r--r-- | activerecord/lib/active_record/validations/uniqueness.rb | 14 |
1 files changed, 7 insertions, 7 deletions
diff --git a/activerecord/lib/active_record/validations/uniqueness.rb b/activerecord/lib/active_record/validations/uniqueness.rb index 608d072e48..58273509be 100644 --- a/activerecord/lib/active_record/validations/uniqueness.rb +++ b/activerecord/lib/active_record/validations/uniqueness.rb @@ -55,6 +55,10 @@ module ActiveRecord value = value.attributes[reflection.klass.primary_key] unless value.nil? end + if value.nil? + return klass.unscoped.where!(attribute => value) + end + # the attribute may be an aliased attribute if klass.attribute_alias?(attribute) attribute = klass.attribute_alias(attribute) @@ -66,18 +70,14 @@ module ActiveRecord column = klass.columns_hash[attribute_name] cast_type = klass.type_for_attribute(attribute_name) - comparison = if !options[:case_sensitive] && !value.nil? + comparison = if !options[:case_sensitive] # will use SQL LOWER function before comparison, unless it detects a case insensitive collation klass.connection.case_insensitive_comparison(table, attribute, column, value) else klass.connection.case_sensitive_comparison(table, attribute, column, value) end - if value.nil? - klass.unscoped.where(comparison) - else - bind = Relation::QueryAttribute.new(attribute_name, value, cast_type) - klass.unscoped.where(comparison, bind) - end + bind = Relation::QueryAttribute.new(attribute_name, value, cast_type) + klass.unscoped.where!(comparison, bind) end def scope_relation(record, relation) |