diff options
3 files changed, 18 insertions, 26 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb index d8bd33f72a..bfbf953a37 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb @@ -311,9 +311,9 @@ module ActiveRecord # an UPDATE statement, so in the mysql adapters we redefine this to do that. def join_to_update(update, select) #:nodoc: subselect = select.ast.clone - subselect.cores.last.projections = [update.ast.key] + subselect.cores.last.projections = [update.key] - update.where update.ast.key.in(subselect) + update.where update.key.in(subselect) end protected diff --git a/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb index 460745fba0..379ba162ed 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb @@ -584,18 +584,14 @@ module ActiveRecord # in the form of a subsubquery. Ugh! def join_to_update(update, select) #:nodoc: if select.limit || select.offset || select.orders.any? - subsubselect = select.ast.clone - subsubselect.cores.last.projections = [update.ast.key] - subsubselect = Arel::Nodes::TableAlias.new( - Arel::Nodes::Grouping.new(subsubselect), - '__active_record_temp' - ) - - subselect = Arel::Nodes::SelectCore.new - subselect.from = subsubselect - subselect.projections << Arel::Table.new('__active_record_temp')[update.ast.key.name] - - update.where update.ast.key.in(subselect) + subsubselect = select.clone + subsubselect.ast.cores.last.projections = [update.key] + + subselect = Arel::SelectManager.new(select.engine) + subselect.project Arel.sql(update.key.name) + subselect.from subsubselect.as('__active_record_temp') + + update.where update.key.in(subselect) else update.table select.ast.cores.last.source update.wheres = select.constraints diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index 4581c16d25..a33e2d8cb0 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -498,18 +498,14 @@ module ActiveRecord # in the form of a subsubquery. Ugh! def join_to_update(update, select) #:nodoc: if select.limit || select.offset || select.orders.any? - subsubselect = select.ast.clone - subsubselect.cores.last.projections = [update.ast.key] - subsubselect = Arel::Nodes::TableAlias.new( - Arel::Nodes::Grouping.new(subsubselect), - '__active_record_temp' - ) - - subselect = Arel::Nodes::SelectCore.new - subselect.from = subsubselect - subselect.projections << Arel::Table.new('__active_record_temp')[update.ast.key.name] - - update.where update.ast.key.in(subselect) + subsubselect = select.clone + subsubselect.ast.cores.last.projections = [update.key] + + subselect = Arel::SelectManager.new(select.engine) + subselect.project Arel.sql(update.key.name) + subselect.from subsubselect.as('__active_record_temp') + + update.where update.key.in(subselect) else update.table select.ast.cores.last.source update.wheres = select.constraints |