less than 1 minute read

Normally in a Rails application, you want to cache three things: rendered template snippets, controller logic and model results. Rails has solutions for each one, but it was difficult to balance each tier. Often times, people will cache templates but still let their controller logic run.

With the new Active Record functionality and Relation objects, it makes simple to get a lot of speed out of just changing a little application logic. The trick is that queries are not run until you call it from the view with the .all .each methods on the Relation object, so if you would like to push the cache logic to the view layer, simply use a model query without these methods and use as like this:

In the controller:

def index
  @posts = Post.where(:published => true).limit(20)
end

In the view:

<% @posts.each do |post| %>
  <%= post.title %>
<% end %>

In this case, the query will be executed when we call .each. If we wrap that in a cache block, it will be executed only the cache is invalidated.

Updated:

Comments