Rails partial caching is showing view changes despite no model changes -
following guide "agile web development rails 4." covers caching product catalog re-render products changed.
i edited (config/environments/development.rb):
config.action_controller.perform_caching = true
added code return updated product: (app/models/product.rb)
def self.latest product.order(:updated_at).latest end
lastly updated store index cache: (app/views/store/index.html.erb)
<h1>your pragmatic catalog</h1> <% cache ['store', product.latest] %> <% @products.each |product| %> <% cache ['entry', product] %> <div class="entry"> <%= image_tag(product.image_url) %> <h3><%= product.title %></h3> <%= sanitize(product.description) %> <div class="price_line"> <span class="price"><%= number_to_currency(product.price) %></span> </div> </div> <% end %> <% end %> <% end %>
the book states way test if caching working change view did add lorem ipsum within 1 or both cache tags browser shows change on refresh... frustrating! clue?
your cache key collection not changing. happens because cache ['store', product.latest]
not clever expect. if have @ log you'll see cache key used literal including activerecord::relation::products.
use last updated_at value cache ['store', product.latest.max(:updated_at)]
better results.
in rails 5 easier due the addition of cache_key
activerecord::relation few weeks ago.
Comments
Post a Comment