Context
class Comment
belongs_to :post, counter_cache: true
end
With the code above, every time a comment is added or removed from a post, the column comments_count in the posts table is increased or decreased.
When the comments_count of a Post is changed, its updated_at is not changed. That is because, by default, counter cache doesn’t touch the model.
Sometimes, though, it’s useful to also touch the Post, for instance if it’s displayed in a cached view and the cache is based on its updated_at. In those cases, this syntax works:
class Comment
belongs_to :post, counter_cache: true, touch: true
end
Problem
The last snippet of code touches the parent Post every time a comment is changed. For instance, if the comment’s text changes, the parent Post is touched. Even though this snippet solves the problem (a cached Post becomes expired), it’s a little too much.
Feature proposal
class Comment
belongs_to :post, counter_cache: { touch: true }
end
This code would only touch the parent Post when the counter cache column is updated.
Technical notes
The counter_cache option already accepts a Hash, see these lines of code.
If the Rails core team accepts this feature proposal, I can submit a PR which