From 82b8061ca2f003edd4ca69c7607c87851ca50d97 Mon Sep 17 00:00:00 2001 From: Alex Ghiculescu Date: Mon, 25 Jul 2022 15:14:21 -0500 Subject: [PATCH] Mention `Arel.sql` in `update_all` docs [docs] This is needed when a string is input, to avoid type casting. Fixes https://github.com/rails/rails/issues/45658 --- activerecord/lib/active_record/relation.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 0848e7de16..43921095db 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -448,7 +448,8 @@ def _exec_scope(...) # :nodoc: # # ==== Parameters # - # * +updates+ - A string, array, or hash representing the SET part of an SQL statement. + # * +updates+ - A string, array, or hash representing the SET part of an SQL statement. Any strings provided will + # be type cast, unless you use `Arel.sql`. (Don't pass user-provided values to `Arel.sql`.) # # ==== Examples # @@ -463,6 +464,9 @@ def _exec_scope(...) # :nodoc: # # # Update all invoices and set the number column to its id value. # Invoice.update_all('number = id') + # + # # Update all books with 'Rails' in their title + # Book.where('title LIKE ?', '%Rails%').update_all(title: Arel.sql("title + ' - volume 1'")) def update_all(updates) raise ArgumentError, "Empty list of attributes to change" if updates.blank?