Named_scope and caching?

Like many I was excited about the named_scope stuff being rolled into
Rails 2.1, but now that I try and go through and replace some of my
self.find-like methods into named_scopes I am encountering a
problem… how do I cache them?

I have a rather large dataset so I try and cache as much as possible.
To that end I have this:

def self.global_lists
{ FilmList.find(:all, :conditions => [“user_id is null”])}

Immediately I can see that I could write:

named_scope :global_lists_ns, :conditions => {:user_id => nil}

but, how can I cache this apart from wrapping it in a self. method and
having now:

def self.global_lists
::Rails.cache.fetch(“global_lists”) { FilmList.global_lists_ns}

Which means I have functionality in two places (arguably ok as I could
use one to access the db and one to access the cache).

Any ideas? Or is this what it is supposed to be like?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs