From 04d1c3716b5bfa133a0b1ed937649a0829ac5f22 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Mon, 19 Jan 2015 15:17:43 -0700 Subject: Fix bind value copying from subqueried relations With the old implementation, the bind values were created, and then we search the attributes for `Relation` objects, and merge them. This completely ignores the order that the actual `where` clause will use. If all non-relation where parameters are before the relations, it will work. However, if we query on both a relation and a value, with the value coming second, it breaks. The order of the hash should not affect the final query (especially since hashes being ordered is an implementation detail) --- .../lib/active_record/relation/predicate_builder.rb | 2 ++ activerecord/lib/active_record/relation/query_methods.rb | 13 ------------- 2 files changed, 2 insertions(+), 13 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/relation/predicate_builder.rb b/activerecord/lib/active_record/relation/predicate_builder.rb index f1cb4e1bee..2860a30f99 100644 --- a/activerecord/lib/active_record/relation/predicate_builder.rb +++ b/activerecord/lib/active_record/relation/predicate_builder.rb @@ -41,6 +41,8 @@ module ActiveRecord attrs, bvs = associated_predicate_builder(column_name).create_binds(value) result[column_name] = attrs binds += bvs + when Relation + binds += value.arel.bind_values + value.bind_values end end diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index c3c1b6c961..c34e4bfb9b 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -949,7 +949,6 @@ module ActiveRecord self.bind_values += bind_values attributes = @klass.send(:expand_hash_conditions_for_aggregates, tmp_opts) - add_relations_to_bind_values(attributes) predicate_builder.build_from_hash(attributes) else @@ -1119,17 +1118,5 @@ module ActiveRecord raise ArgumentError, "The method .#{method_name}() must contain arguments." end end - - def add_relations_to_bind_values(attributes) - if attributes.is_a?(Hash) - attributes.each_value do |value| - if value.is_a?(ActiveRecord::Relation) - self.bind_values += value.arel.bind_values + value.bind_values - else - add_relations_to_bind_values(value) - end - end - end - end end end -- cgit v1.2.3