diff options
author | Jeremy Daer <jeremydaer@gmail.com> | 2017-02-06 23:56:09 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-06 23:56:09 -0700 |
commit | f6d68408a0bd6f2f1a32627ab8b8e4cd68eeb43e (patch) | |
tree | 403cef2edcc9a33760998b13347b36a1eac7344d /activerecord/lib/active_record/validations | |
parent | 28977f1fa3d7b15c1608174a165e60b71ddf3995 (diff) | |
parent | a928832550dcfdec22bc99941910302c535b1925 (diff) | |
download | rails-f6d68408a0bd6f2f1a32627ab8b8e4cd68eeb43e.tar.gz rails-f6d68408a0bd6f2f1a32627ab8b8e4cd68eeb43e.tar.bz2 rails-f6d68408a0bd6f2f1a32627ab8b8e4cd68eeb43e.zip |
Merge pull request #26378 from kamipo/decouple_building_arel_ast_for_uniqueness_validator
Decouple the building Arel ASTs for uniqueness validator
Diffstat (limited to 'activerecord/lib/active_record/validations')
-rw-r--r-- | activerecord/lib/active_record/validations/uniqueness.rb | 32 |
1 files changed, 1 insertions, 31 deletions
diff --git a/activerecord/lib/active_record/validations/uniqueness.rb b/activerecord/lib/active_record/validations/uniqueness.rb index 9e8edfbfaf..154cf5f1a4 100644 --- a/activerecord/lib/active_record/validations/uniqueness.rb +++ b/activerecord/lib/active_record/validations/uniqueness.rb @@ -50,37 +50,7 @@ module ActiveRecord end def build_relation(klass, attribute, value) - if reflection = klass._reflect_on_association(attribute) - attribute = reflection.foreign_key - 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) - end - - attribute_name = attribute.to_s - - table = klass.arel_table - column = klass.columns_hash[attribute_name] - cast_type = klass.type_for_attribute(attribute_name) - - 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 - klass.unscoped.tap do |scope| - parts = [comparison] - binds = [Relation::QueryAttribute.new(attribute_name, value, cast_type)] - scope.where_clause += Relation::WhereClause.new(parts, binds) - end + klass.unscoped.where!({ attribute => value }, options) end def scope_relation(record, relation) |