If you use postgres, you can run update_all with "join" from the model, for example:
Task.connection.update_sql("UPDATE tasks AS t SET project_id = p.id FROM projects AS p WHERE t.ref_id = p.ref_id")
Note: the call to update_sql deprecated for Rails 5 and should be switched to update
Since you are sending sql directly, you need to make sure that you use the syntax appropriate for your database.
source share