---
title: "Rails 6 adds ActiveRecord::Relation#touch_all"
description:
  "Rails 6 added pick method on ActiveRecord::Relation#touch_all to touch
  multiple records in a go"
canonical_url: "https://www.bigbinary.com/blog/rails-6-adds-activerecord-relation-touch-all"
markdown_url: "https://www.bigbinary.com/blog/rails-6-adds-activerecord-relation-touch-all.md"
---

# Rails 6 adds ActiveRecord::Relation#touch_all

Rails 6 added pick method on ActiveRecord::Relation#touch_all to touch multiple
records in a go

- Author: Amit Choudhary
- Published: March 12, 2019
- Categories: Rails 6, Rails

Before moving forward, we need to understand what the
[touch](https://api.rubyonrails.org/v5.2/classes/ActiveRecord/Persistence.html#method-i-touch)
method does.
[touch](https://api.rubyonrails.org/v5.2/classes/ActiveRecord/Persistence.html#method-i-touch)
is used to update the `updated_at` timestamp by defaulting to the current time.
It also takes custom time or different columns as parameters.

Rails 6 has added [touch_all](https://github.com/rails/rails/pull/31513) on
ActiveRecord::Relation to touch multiple records in one go. Before Rails 6, we
needed to iterate all records using an iterator to achieve this result.

Let's take an example in which we call
[touch_all](https://github.com/rails/rails/pull/31513) on all user records.

#### Rails 5.2

```ruby
>> User.count
SELECT COUNT(\*) FROM "users"

=> 3

>> User.all.touch_all

=> Traceback (most recent call last):1: from (irb):2
NoMethodError (undefined method 'touch_all' for #<User::ActiveRecord_Relation:0x00007fe6261f9c58>)

>> User.all.each(&:touch)
SELECT "users".* FROM "users"
begin transaction
  UPDATE "users" SET "updated_at" = ? WHERE "users"."id" = ?  [["updated_at", "2019-03-05 17:45:51.495203"], ["id", 1]]
commit transaction
begin transaction
  UPDATE "users" SET "updated_at" = ? WHERE "users"."id" = ?  [["updated_at", "2019-03-05 17:45:51.503415"], ["id", 2]]
commit transaction
begin transaction
  UPDATE "users" SET "updated_at" = ? WHERE "users"."id" = ?  [["updated_at", "2019-03-05 17:45:51.509058"], ["id", 3]]
commit transaction

=> [#<User id: 1, name: "Sam", created_at: "2019-03-05 16:09:29", updated_at: "2019-03-05 17:45:51">, #<User id: 2, name: "John", created_at: "2019-03-05 16:09:43", updated_at: "2019-03-05 17:45:51">, #<User id: 3, name: "Mark", created_at: "2019-03-05 16:09:45", updated_at: "2019-03-05 17:45:51">]
```

#### Rails 6.0.0.beta2

```ruby
>> User.count
SELECT COUNT(*) FROM "users"

=> 3

>> User.all.touch_all
UPDATE "users" SET "updated_at" = ?  [["updated_at", "2019-03-05 16:08:47.490507"]]

=> 3
```

[touch_all](https://github.com/rails/rails/pull/31513) returns count of the
records on which it is called.

[touch_all](https://github.com/rails/rails/pull/31513) also takes a custom time
or different columns as parameters.

#### Rails 6.0.0.beta2

```ruby
>> User.count
SELECT COUNT(*) FROM "users"

=> 3

>> User.all.touch_all(time: Time.new(2019, 3, 2, 1, 0, 0))
UPDATE "users" SET "updated_at" = ?  [["updated_at", "2019-03-02 00:00:00"]]

=> 3

>> User.all.touch_all(:created_at)
UPDATE "users" SET "updated_at" = ?, "created_at" = ?  [["updated_at", "2019-03-05 17:55:41.828347"], ["created_at", "2019-03-05 17:55:41.828347"]]

=> 3
```

Here is the relevant [pull request](https://github.com/rails/rails/pull/31513).

## Links

- [Human page](https://www.bigbinary.com/blog/rails-6-adds-activerecord-relation-touch-all)
