Call Montréal, Canada 524-949-6869

Blog

Monday, October 18, 2010

Cached Key for ActiveRecord Collections

Last night I was doing some optimizations on Heroku utilizing Vanish, their reverse proxy server, and wanted to set an etag for an ActiveRecord collection. Basically I wanted to do something like this:

def index
    @videos = Video.published

    fresh_when :etag => @videos
    headers['Cache-Control'] = 'public'
   end

Surprisingly an ActiveRecord collection does not support the cache_key. Fortunately Jim Gay has written a gem, group_cache_key, to resolve this shortcoming. Just add the gem to your Gemfile and bundle install.

Developing Ruby on Rails applications and custom Content Management Systems in Montréal. Contact Firsthand.