touch is used to update the
Rails 6 has added
touch_all method to
ActiveRecord::Relation in order to
touch multiple records at once.
Before Rails 6, we needed to loop over all the records
touch for each record.
This results N +1 queries.
There are multitude of wasy in which touch could be used:
We can now use
touch_all to overcome this looping operation.
touch_all accepts two arguments:
- column names
updated_at column by default, same as
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:
touch_all provides us with a faster and simpler way to touch a collection of objects.