update_all
![Very extensive documentation Importance_5](https://d2vfyqvduarcvs.cloudfront.net/images/importance_5.png?1349367920)
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0 (0)
- 3.0.9 (-3)
- 3.1.0 (16)
- 3.2.1 (0)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2 (-38)
- 4.1.8 (0)
- 4.2.1 (6)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (-3)
- 5.1.7 (6)
- 5.2.3 (0)
- 6.0.0 (8)
- 6.1.3.1 (2)
- 6.1.7.7 (0)
- 7.0.0 (0)
- 7.1.3.2 (16)
- 7.1.3.4 (0)
- What's this?
update_all(updates)
public
Updates all records in the current relation with details given. This method constructs a single SQL UPDATE statement and sends it straight to the database. It does not instantiate the involved models and it does not trigger Active Record callbacks or validations. However, values passed to #update_all will still go through Active Record’s normal type casting and serialization. Returns the number of rows affected.
Note: As Active Record callbacks are not triggered, this method will not automatically update updated_at/updated_on columns.
Parameters
-
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
# Update all customers with the given attributes Customer.update_all wants_email: true # Update all books with 'Rails' in their title Book.where('title LIKE ?', '%Rails%').update_all(author: 'David') # Update all books that match conditions, but limit it to 5 ordered by date Book.where('title LIKE ?', '%Rails%').order(:created_at).limit(5).update_all(author: 'David') # 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'"))
![Default_avatar_30](https://www.gravatar.com/avatar/66b8ecb6f2cd9f32b657af75c5feebba?size=30&default=http://apidock.com/images/default_avatar_30.png)
Timestamps
Note that ActiveRecord will not update the timestamp fields (updated_at/updated_on) when using update_all().
![Default_avatar_30](https://www.gravatar.com/avatar/e0fd5e98d98e9415e5fa94ac1155ce37?size=30&default=http://apidock.com/images/default_avatar_30.png)
complex conditions
If you need add complex conditions you can use this:
Model.where(:foo => 'bar').where(:attr => 1).update_all("author = 'David'")
![Default_avatar_30](https://www.gravatar.com/avatar/afec58530312f20a44b07159ae70a9e3?size=30&default=http://apidock.com/images/default_avatar_30.png)
Update multiple attributes with raw sql
You can update multiple attributes with sql for each record being updated by using the following syntax:
example
Model.update_all("foo = 'bar', baz = 'bat'")
![Default_avatar_30](https://www.gravatar.com/avatar/16722dcdef9478b025af13f0a638bec4?size=30&default=http://apidock.com/images/default_avatar_30.png)
bug's fixed, though
in 3.1.0. it works in sqlite3 even, via nested query
but 3.0.3, 3.0.7-9 all broken
![Default_avatar_30](https://www.gravatar.com/avatar/16722dcdef9478b025af13f0a638bec4?size=30&default=http://apidock.com/images/default_avatar_30.png)
bug?
beware, update_all silently ignores :limit and :order option in 3.0.8.
I’ve fixed my code temporarily with
update_all "foo=1 where #{myscope.where_values} limit 1"