# Named\_scope and caching?

**URL:** <https://www.ruby-forum.com/t/named-scope-and-caching/144811>\
**Category:** Rails\
**Created:** [August 2, 2008, 2:11pm UTC](https://www.ruby-forum.com/t/named-scope-and-caching/144811 "2008-08-02T14:11:53Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![phil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@phil](https://www.ruby-forum.com/u/phil)\
**Post date:** [August 2, 2008, 2:11pm UTC](https://www.ruby-forum.com/t/named-scope-and-caching/144811/1 "2008-08-02T14:11:53Z")

</div>

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  
::Rails.cache.fetch(“global\_lists”)  
{ FilmList.find(:all, :conditions =\> [“user\_id is null”])}  
end

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}  
end

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?
