From 1cb684c2ef12eea5661b17903f4986edfba0a464 Mon Sep 17 00:00:00 2001
From: Konstantin Haase <konstantin.mailinglists@googlemail.com>
Date: Tue, 4 Sep 2012 15:50:09 +0200
Subject: update caching guide: stale? can also figure out last_modified on its
 own

---
 guides/source/caching_with_rails.textile | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

(limited to 'guides/source/caching_with_rails.textile')

diff --git a/guides/source/caching_with_rails.textile b/guides/source/caching_with_rails.textile
index f6d6c8b550..9f1ac18814 100644
--- a/guides/source/caching_with_rails.textile
+++ b/guides/source/caching_with_rails.textile
@@ -439,7 +439,7 @@ class ProductsController < ApplicationController
 
     # If the request is stale according to the given timestamp and etag value
     # (i.e. it needs to be processed again) then execute this block
-    if stale?(:last_modified => @product.updated_at.utc, :etag => @product)
+    if stale?(:last_modified => @product.updated_at.utc, :etag => @product.cache_key)
       respond_to do |wants|
         # ... normal response processing
       end
@@ -453,6 +453,17 @@ class ProductsController < ApplicationController
 end
 </ruby>
 
+Instead of a options hash, you can also simply pass in a model, Rails will use the methods +updated_at+ and +cache_key+ for setting +last_modified+ and +etag+:
+
+<ruby>
+class ProductsController < ApplicationController
+  def show
+    @product = Product.find(params[:id])
+    respond_with(@product) if stale?(@product)
+  end
+end
+</ruby>
+
 If you don't have any special response processing and are using the default rendering mechanism (i.e. you're not using respond_to or calling render yourself) then you’ve got an easy helper in fresh_when:
 
 <ruby>
-- 
cgit v1.2.3

