You can use the ActiveRecord touch method. This is especially useful if you have one parent record with many child records. Each time child records are saved or destroyed, the parent record will have the updated_at field set to the current time.
class User < ApplicationRecord has_many :phones has_many :addresses end class Phone < ApplicationRecord belongs_to :user, touch: true end class Address < ApplicationRecord belongs_to :user, touch: true end
At any time when the address or phone is updated, the user updated_at will be set to the current time.
To check when the last update for the current user occurred in all of its tables, follow these steps:
@last_updated = @user.updated_at
For small recording overheads, you get many benefits with simpler queries when checking the validity period of your cache. The documentation on this subject can be found in the section belongs to the parameters .
source share