# Class variables in a model are ok to cache relatively static records within the scope of a brower ca

**URL:** <https://www.ruby-forum.com/t/class-variables-in-a-model-are-ok-to-cache-relatively-static-records-within-the-scope-of-a-brower-ca/158176>\
**Category:** Rails\
**Created:** [January 20, 2009, 6:04am UTC](https://www.ruby-forum.com/t/class-variables-in-a-model-are-ok-to-cache-relatively-static-records-within-the-scope-of-a-brower-ca/158176 "2009-01-20T06:04:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [January 20, 2009, 6:04am UTC](https://www.ruby-forum.com/t/class-variables-in-a-model-are-ok-to-cache-relatively-static-records-within-the-scope-of-a-brower-ca/158176/1 "2009-01-20T06:04:15Z")

</div>

Hi,

Class variables in a model are ok to cache relatively static records  
within  
the scope of a brower call aren’t they? That is, the results get  
“refreshed” effectively each browser call (i.e. per browser call, per  
mongrel process). Example of what I’m talking about is below.

@@all\_records = nil  
def self.all\_records  
@@all\_records ||= Automatch.find(:all) # say only 10 records,  
relatively static  
end

–  
Greg  
[http://blog.gregnet.org/](http://blog.gregnet.org/)

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [January 20, 2009, 6:25am UTC](https://www.ruby-forum.com/t/class-variables-in-a-model-are-ok-to-cache-relatively-static-records-within-the-scope-of-a-brower-ca/158176/2 "2009-01-20T06:25:18Z")

</div>

Greg H. wrote:

> Class variables in a model are ok to cache relatively static records  
> within the scope of a brower call aren’t they? That is, the results get  
> “refreshed” effectively each browser call (i.e. per browser call, per  
> mongrel process). Example of what I’m talking about is below.
> 
> @@all\_records = nil  
> def self.all\_records  
> @@all\_records ||= Automatch.find(:all) # say only 10 records,  
> relatively static  
> end

Technically yes, and esthetically no.

You are “coupling” the state of your model to its current location in  
control  
flow. If control flow changes, your coupling might break. If, for  
example, you  
added a new feature that required two model objects, extant at the same  
time,  
then their @@all\_records might accidentally conflict. Decoupled code is  
easier  
to safely change - that’s essentially the definition of “decoupled”.

Take out @@all\_records = nil entirely, then use @all\_records =  
Automatch.find(:all). If your controller and similar code is also  
decoupled, it  
won’t create more instances of your model than it needs, and they will  
be efficient.

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [January 20, 2009, 6:42am UTC](https://www.ruby-forum.com/t/class-variables-in-a-model-are-ok-to-cache-relatively-static-records-within-the-scope-of-a-brower-ca/158176/3 "2009-01-20T06:42:51Z")

</div>

thanks - that makes sense - interestly enough the code I had, whilst it  
worked from the console, didn’t behave the same way from an RSpec  
test…anyway I’ll follow your advice

On Tue, Jan 20, 2009 at 3:24 PM, Phlip [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > @@all_records ||= Automatch.find(:all) # say only 10 records,
> > 
> > ```
> 
> time,
> 
> >

–  
Greg  
[http://blog.gregnet.org/](http://blog.gregnet.org/)

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [January 20, 2009, 11:44am UTC](https://www.ruby-forum.com/t/class-variables-in-a-model-are-ok-to-cache-relatively-static-records-within-the-scope-of-a-brower-ca/158176/4 "2009-01-20T11:44:32Z")

</div>

On 20 Jan 2009, at 05:03, Greg H. wrote:

> ```
> @@all_records ||= Automatch.find(:all) # say only 10  
> 
> ```
> 
> records, relatively static  
> end

In development yes, since the class is reloaded. In production no  
since the class isn’t reloaded so @@all\_record is only ever set once  
(minus potential race conditions upon initialization)

Fred
