diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2013-07-02 11:12:17 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2013-07-02 11:22:22 -0700 |
commit | 8b9f16fcb350006a8e903bd52be04f30ae8b8d89 (patch) | |
tree | afe7e248bfddfc88525879706e0d2f7503049bcd /activerecord | |
parent | ede2f0c71214fa9564df3cbb1f3e60796f1b39bd (diff) | |
download | rails-8b9f16fcb350006a8e903bd52be04f30ae8b8d89.tar.gz rails-8b9f16fcb350006a8e903bd52be04f30ae8b8d89.tar.bz2 rails-8b9f16fcb350006a8e903bd52be04f30ae8b8d89.zip |
resolve aliases before passing the hash to the predicate builder
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/relation/predicate_builder.rb | 14 | ||||
-rw-r--r-- | activerecord/lib/active_record/relation/query_methods.rb | 1 | ||||
-rw-r--r-- | activerecord/lib/active_record/sanitization.rb | 1 |
3 files changed, 12 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/relation/predicate_builder.rb b/activerecord/lib/active_record/relation/predicate_builder.rb index 59a3130939..08ef899f68 100644 --- a/activerecord/lib/active_record/relation/predicate_builder.rb +++ b/activerecord/lib/active_record/relation/predicate_builder.rb @@ -1,15 +1,21 @@ module ActiveRecord class PredicateBuilder # :nodoc: + def self.resolve_column_aliases(klass, hash) + hash = hash.dup + hash.keys.grep(Symbol) do |key| + if klass.attribute_alias? key + hash[klass.attribute_alias(key)] = hash.delete key + end + end + hash + end + def self.build_from_hash(klass, attributes, default_table) queries = [] attributes.each do |column, value| table = default_table - if column.is_a?(Symbol) && klass.attribute_alias?(column) - column = klass.attribute_alias(column) - end - if value.is_a?(Hash) if value.empty? queries << '1=0' diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 05e92bea33..1327cc3c34 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -891,6 +891,7 @@ module ActiveRecord when String, Array [@klass.send(:sanitize_sql, other.empty? ? opts : ([opts] + other))] when Hash + opts = PredicateBuilder.resolve_column_aliases klass, opts attributes = @klass.send(:expand_hash_conditions_for_aggregates, opts) attributes.values.grep(ActiveRecord::Relation) do |rel| diff --git a/activerecord/lib/active_record/sanitization.rb b/activerecord/lib/active_record/sanitization.rb index 0ed97b66d6..31e294022f 100644 --- a/activerecord/lib/active_record/sanitization.rb +++ b/activerecord/lib/active_record/sanitization.rb @@ -86,6 +86,7 @@ module ActiveRecord # { address: Address.new("123 abc st.", "chicago") } # # => "address_street='123 abc st.' and address_city='chicago'" def sanitize_sql_hash_for_conditions(attrs, default_table_name = self.table_name) + attrs = PredicateBuilder.resolve_column_aliases self, attrs attrs = expand_hash_conditions_for_aggregates(attrs) table = Arel::Table.new(table_name, arel_engine).alias(default_table_name) |