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 3d05f5273606967e17cef23f49ec32942d4f9bfd..848aeb821c598a1211c22ddd6a70180af7e40efa 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb @@ -344,12 +344,18 @@ def sanitize_limit(limit) # The default strategy for an UPDATE with joins is to use a subquery. This doesn't work # on MySQL (even when aliasing the tables), but MySQL allows using JOIN directly in # an UPDATE statement, so in the MySQL adapters we redefine this to do that. - def join_to_update(update, select, key) # :nodoc: + def join_to_update(update, select) #:nodoc: + key = update.key subselect = subquery_for(key, select) update.where key.in(subselect) end - alias join_to_delete join_to_update + + def join_to_delete(delete, select, key) #:nodoc: + subselect = subquery_for(key, select) + + delete.where key.in(subselect) + end protected diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb index 6fddfb5347bdd5d9fc7b5a857266d747af59d32f..25ba42e5c9184b971ab2aa153d3e7e4c50860c66 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -432,7 +432,7 @@ def exec_rollback_db_transaction #:nodoc: # In the simple case, MySQL allows us to place JOINs directly into the UPDATE # query. However, this does not allow for LIMIT, OFFSET and ORDER. To support # these, we must use a subquery. - def join_to_update(update, select, key) # :nodoc: + def join_to_update(update, select) #:nodoc: if select.limit || select.offset || select.orders.any? super else diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 316b0d6308a75a9032883a9f9b9800ea5969f812..2cf19c76c5aebf970690d023893857f8ac90222b 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -371,11 +371,11 @@ def update_all(updates) stmt.set Arel.sql(@klass.send(:sanitize_sql_for_assignment, updates)) stmt.table(table) + stmt.key = table[primary_key] if joins_values.any? - @klass.connection.join_to_update(stmt, arel, table[primary_key]) + @klass.connection.join_to_update(stmt, arel) else - stmt.key = table[primary_key] stmt.take(arel.limit) stmt.order(*arel.orders) stmt.wheres = arel.constraints