diff options
Diffstat (limited to 'activerecord/lib/active_record/relation')
3 files changed, 30 insertions, 12 deletions
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index b763e22ec6..fe1ef2e2e3 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -288,7 +288,12 @@ module ActiveRecord def find_one(id) id = id.id if ActiveRecord::Base === id - record = where(primary_key.eq(id)).first + column = primary_key.column + + substitute = connection.substitute_for(column, @bind_values) + relation = where(primary_key.eq(substitute)) + relation.bind_values = [[column, id]] + record = relation.first unless record conditions = arel.where_sql diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 59ce76ea42..9c399d3333 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -6,7 +6,8 @@ module ActiveRecord extend ActiveSupport::Concern attr_accessor :includes_values, :eager_load_values, :preload_values, - :select_values, :group_values, :order_values, :joins_values, :where_values, :having_values, + :select_values, :group_values, :order_values, :joins_values, + :where_values, :having_values, :bind_values, :limit_value, :offset_value, :lock_value, :readonly_value, :create_with_value, :from_value def includes(*args) @@ -62,6 +63,12 @@ module ActiveRecord relation end + def bind(value) + relation = clone + relation.bind_values += [value] + relation + end + def where(opts, *rest) relation = clone relation.where_values += build_where(opts, rest) unless opts.blank? @@ -117,8 +124,10 @@ module ActiveRecord relation end - def extending(*modules, &block) - modules << Module.new(&block) if block_given? + def extending(*modules) + modules << Module.new(&Proc.new) if block_given? + + return self if modules.empty? relation = clone relation.send(:apply_modules, modules.flatten) @@ -233,7 +242,7 @@ module ActiveRecord @implicit_readonly = false arel.project(*selects) else - arel.project(Arel::SqlLiteral.new(@klass.quoted_table_name + '.*')) + arel.project(Arel.sql(@klass.quoted_table_name + '.*')) end end diff --git a/activerecord/lib/active_record/relation/spawn_methods.rb b/activerecord/lib/active_record/relation/spawn_methods.rb index dcddc3dac4..648a02f1cc 100644 --- a/activerecord/lib/active_record/relation/spawn_methods.rb +++ b/activerecord/lib/active_record/relation/spawn_methods.rb @@ -26,19 +26,23 @@ module ActiveRecord merged_relation = merged_relation.joins(r.joins_values) - merged_wheres = @where_values + merged_wheres = @where_values + r.where_values - r.where_values.each do |w| + # Remove duplicates, last one wins. + seen = {} + merged_wheres = merged_wheres.reverse.reject { |w| + nuke = false if w.respond_to?(:operator) && w.operator == :== - merged_wheres = merged_wheres.reject {|p| p.respond_to?(:operator) && p.operator == :== && p.operand1.name == w.operand1.name } + name = w.left.name + nuke = seen[name] + seen[name] = true end - - merged_wheres += [w] - end + nuke + }.reverse merged_relation.where_values = merged_wheres - Relation::SINGLE_VALUE_METHODS.reject {|m| m == :lock}.each do |method| + (Relation::SINGLE_VALUE_METHODS - [:lock]).each do |method| value = r.send(:"#{method}_value") merged_relation.send(:"#{method}_value=", value) unless value.nil? end |