RailsCasts Pro episodes are now free!

Learn more or hide this

Godfrey Chan's Profile

GitHub User: chancancode

Site: http://about.me/godfreychan

Comments by Godfrey Chan

Avatar

Good point. (This will most likely result in a different SQL query due to lazy/last-minute evaluation in AR, probably for the better...)

Avatar

Apparently the DB queries is not the bottleneck for most cases, if you can avoid complex joints, etc... This came as a surprise to me too. (See the linked 37Signals article and the comments)

Avatar

A quick test in the console suggests that this should work (although you probably won't want to do this when you have a lot of objects in you array?)

>> ActiveSupport::Cache::expand_cache_key(["key_test"] + Model.limit(5))
  Model Load (0.3ms) ...
=> "key_test/model/1-20110606182109/model/2-20110606174200/model/3-20110606170234/model/4-20110606170854/model/5-20110604144748"
Avatar

What's a good way to auto-expire the "recent_articles" fragment you have there? Would something like this work?

ruby
<% cache ["recent_articles"] + @recent_articles do %>
<div id="recent_articles">
  <h3>Recent Articles</h3>
  <ul>
  <% @recent_articles.each do |article| %>
    <% cache ["recent", article] do %>
      <li><%= link_to article.name, article %></li>
    <% end %>
  <% end %>
  </ul>
</div>
<% end %>