From 58e5ba0a5f87f70e0a4d4aa7eb26bc643db55e0c Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 14 Jan 2014 16:21:10 -0800 Subject: take in to account existing bind parameters when building the bind list --- .../lib/active_record/relation/query_methods.rb | 41 +++++++++++----------- 1 file changed, 20 insertions(+), 21 deletions(-) (limited to 'activerecord/lib/active_record/relation') diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 028043fece..159ad1dbe3 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -919,13 +919,13 @@ module ActiveRecord [@klass.send(:sanitize_sql, other.empty? ? opts : ([opts] + other))] when Hash opts = PredicateBuilder.resolve_column_aliases(klass, opts) - temp_opts = opts.dup attributes = @klass.send(:expand_hash_conditions_for_aggregates, opts) - create_binds(temp_opts) - temp_opts = substitute_opts(temp_opts) + bv_len = bind_values.length + tmp_opts, bind_values = create_binds(opts, bv_len) + self.bind_values += bind_values - attributes = @klass.send(:expand_hash_conditions_for_aggregates, temp_opts) + attributes = @klass.send(:expand_hash_conditions_for_aggregates, tmp_opts) attributes.values.grep(ActiveRecord::Relation) do |rel| self.bind_values += rel.bind_values end @@ -936,28 +936,27 @@ module ActiveRecord end end - def create_binds(temp_opts) - binds = [] - temp_opts.map do |column, value| + def create_binds(opts, idx) + bindable, non_binds = opts.partition do |column, value| case value - when String, Integer - if @klass.column_names.include? column.to_s - binds.push([@klass.columns_hash[column.to_s], value]) - end + when String, Integer + @klass.columns_hash.include? column.to_s + else + false end end - self.bind_values += binds - end - def substitute_opts(temp_opts) - temp_opts = temp_opts.each_with_index do |(column,value), index| - if @klass.columns_hash[column.to_s] != nil - case value - when String, Integer - temp_opts[column] = connection.substitute_at(column, index) - end - end + new_opts = {} + binds = [] + + bindable.each_with_index do |(column,value), index| + binds.push [@klass.columns_hash[column.to_s], value] + new_opts[column] = connection.substitute_at(column, index + idx) end + + non_binds.each { |column,value| new_opts[column] = value } + + [new_opts, binds] end def build_from -- cgit v1.2.3