[Feature Proposal] Support timestamps from joined relations in cached_version!

Hey folks, I have a query of a has_many relation, that I want to cache. The cache_key should be invalid based on max(updated_at) but also based on max(belongs_to.updated_at).

Some code for illustration:

class Article < ApplicationRecord 
  has_many :comments
end

class Comment < ApplicationRecord 
  belongs_to :article, touch: true
end

The following will work because I touch Article on every Comment update

Article.includes(:comments).all.cache_version

But

Comment.includes(:article).all.cache_version

will not get a new cache_version when article updated

I would like to propose, to

  • support multiple timestamp fields in ActiveRecord::Relation.cache_version
  • support table names in ActiveRecord::Relation.cache_version
Comment.includes(:article).cache_version('articles.updated_at','comments.updated_at')

What do you think?