diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2018-09-28 01:03:13 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2018-09-28 01:53:01 +0900 |
commit | 8e123847e8ba0a59aafa708950a4887a669311b4 (patch) | |
tree | 0569dae364347c8a1c5f204d46856f7588d0f21b | |
parent | 140ec68c0dc157d1b3cd481f48dd403b58c74923 (diff) | |
download | rails-8e123847e8ba0a59aafa708950a4887a669311b4.tar.gz rails-8e123847e8ba0a59aafa708950a4887a669311b4.tar.bz2 rails-8e123847e8ba0a59aafa708950a4887a669311b4.zip |
Make `update_all` preparable
Before:
```
Pet Update All (0.8ms) UPDATE `pets` LEFT OUTER JOIN `toys` ON `toys`.`pet_id` = `pets`.`pet_id` SET `pets`.`name` = 'Bob' WHERE `toys`.`name` = ? [["name", "Bone"]]
```
After:
```
Pet Update All (1.1ms) UPDATE `pets` LEFT OUTER JOIN `toys` ON `toys`.`pet_id` = `pets`.`pet_id` SET `pets`.`name` = ? WHERE `toys`.`name` = ? [["name", "Bob"], ["name", "Bone"]]
```
-rw-r--r-- | activerecord/lib/active_record/relation.rb | 16 | ||||
-rw-r--r-- | activerecord/lib/arel/visitors/mysql.rb | 4 |
2 files changed, 18 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 806f8a1cbb..771ca952e1 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -348,10 +348,14 @@ module ActiveRecord end stmt = Arel::UpdateManager.new - - stmt.set Arel.sql(@klass.sanitize_sql_for_assignment(updates, table.name)) stmt.table(table) + if updates.is_a?(Hash) + stmt.set _substitute_values(updates) + else + stmt.set Arel.sql(klass.sanitize_sql_for_assignment(updates, table.name)) + end + if has_join_values? || offset_value @klass.connection.join_to_update(stmt, arel, arel_attribute(primary_key)) else @@ -625,6 +629,14 @@ module ActiveRecord end private + def _substitute_values(values) + values.map do |name, value| + attr = arel_attribute(name) + type = klass.type_for_attribute(attr.name) + bind = predicate_builder.build_bind_attribute(attr.name, type.cast(value)) + [attr, bind] + end + end def has_join_values? joins_values.any? || left_outer_joins_values.any? diff --git a/activerecord/lib/arel/visitors/mysql.rb b/activerecord/lib/arel/visitors/mysql.rb index 37bfb661f0..ee75b6bb25 100644 --- a/activerecord/lib/arel/visitors/mysql.rb +++ b/activerecord/lib/arel/visitors/mysql.rb @@ -37,6 +37,10 @@ module Arel # :nodoc: all visit o.expr, collector end + def visit_Arel_Nodes_UnqualifiedColumn(o, collector) + visit o.expr, collector + end + ### # :'( # http://dev.mysql.com/doc/refman/5.0/en/select.html#id3482214 |