This blog is part of our Rails 6 series.
Before moving forward, we need to understand what the touch method does. 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 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 on all user records.
Rails 5.2
1>> User.count 2SELECT COUNT(\*) FROM "users" 3 4=> 3 5 6>> User.all.touch_all 7 8=> Traceback (most recent call last):1: from (irb):2 9NoMethodError (undefined method 'touch_all' for #<User::ActiveRecord_Relation:0x00007fe6261f9c58>) 10 11>> User.all.each(&:touch) 12SELECT "users".* FROM "users" 13begin transaction 14 UPDATE "users" SET "updated_at" = ? WHERE "users"."id" = ? [["updated_at", "2019-03-05 17:45:51.495203"], ["id", 1]] 15commit transaction 16begin transaction 17 UPDATE "users" SET "updated_at" = ? WHERE "users"."id" = ? [["updated_at", "2019-03-05 17:45:51.503415"], ["id", 2]] 18commit transaction 19begin transaction 20 UPDATE "users" SET "updated_at" = ? WHERE "users"."id" = ? [["updated_at", "2019-03-05 17:45:51.509058"], ["id", 3]] 21commit transaction 22 23=> [#<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
1>> User.count 2SELECT COUNT(*) FROM "users" 3 4=> 3 5 6>> User.all.touch_all 7UPDATE "users" SET "updated_at" = ? [["updated_at", "2019-03-05 16:08:47.490507"]] 8 9=> 3
touch_all returns count of the records on which it is called.
touch_all also takes a custom time or different columns as parameters.
Rails 6.0.0.beta2
1>> User.count 2SELECT COUNT(*) FROM "users" 3 4=> 3 5 6>> User.all.touch_all(time: Time.new(2019, 3, 2, 1, 0, 0)) 7UPDATE "users" SET "updated_at" = ? [["updated_at", "2019-03-02 00:00:00"]] 8 9=> 3 10 11>> User.all.touch_all(:created_at) 12UPDATE "users" SET "updated_at" = ?, "created_at" = ? [["updated_at", "2019-03-05 17:55:41.828347"], ["created_at", "2019-03-05 17:55:41.828347"]] 13 14=> 3
Here is the relevant pull request.