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