touch
is used to update the updated_at
timestamp.
Rails 6 has added
touch_all
method to ActiveRecord::Relation
in order to touch
multiple records at once.
Before
Before Rails 6, we needed to loop over all the records
and call touch
for each record.
This results N +1 queries.
There are multitude of ways in which touch could be used:
touch_all
We can now use touch_all
to overcome this looping operation.
touch_all
accepts two arguments:
- column names
time
(optional)
It updates updated_at
column by default, same as touch
.
Default value for time is current time.
In comparison to touch
, this results in a single query instead of N+1 queries.
Similarly, we can utilize touch_all
method in mulitple ways:
Summary
touch_all
provides us with a faster and simpler way to touch a collection of objects.