KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
After studying DHH's and other blog articles about key-based cache expiration and Russian Doll Caching, I am still unsure how to handle one relation type. To be specific, a has_many relationship. I will share the results of my research on a sample app. It is a little bit of story telling, so hang on. Let's say we have the following ActiveRecord models. All we care about is a proper change of the model's cache_key , right? class Article < ActiveRecord::Base attr_accessible :author_id, :body, :title has_many :comments belongs_to :author end class Comment < ActiveRecord::Base attr_accessible :article_id, :author_id, :body belongs_to :author belongs_to :article, touch: true end class Author < ActiveRecord::Base attr_accessible :name has_many :articles has_many :comments end We already have one article, with one comment. Both by a different author. The goal is to have a change in the cache_key for the article in the following cases: Article's body or title changes Its comment's body changes Article's author's name changes Article's comment's author's name changes So by default, we are good for case 1 and 2. 1.9.3-p194 :034 > article.cache_key => "articles/1-20130412185804" 1.9.3-p194 :035 > article.comments.first.update_attribute('body', 'First Post!') 1.9.3-p194 :038 > article.cache_key => "articles/1-20130412185913" But not for case 3. 1.9.3-p194 :040 > article.author.update_attribute('name', 'Adam A.') 1.9.3-p194 :041 > article.cache_key => "articles/1-20130412185913" Let's define a composite cache_key method for Article . class Article < ActiveRecord::Base attr_accessible :author_id, :body, :title has_many :comments belongs_to :author def cache_key [supe
Tags (comma-separated)
Save Edits
Cancel