# Can rails cache a table?

**URL:** <https://www.ruby-forum.com/t/can-rails-cache-a-table/64846>\
**Category:** Rails\
**Created:** [February 9, 2006, 12:40am UTC](https://www.ruby-forum.com/t/can-rails-cache-a-table/64846 "2006-02-09T00:40:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [February 9, 2006, 12:40am UTC](https://www.ruby-forum.com/t/can-rails-cache-a-table/64846/1 "2006-02-09T00:40:11Z")

</div>

We have a few tables that do not change, unless something big happens  
which  
will only happen when it is time to deploy.

Is there a way to get Rails to cache the contents of these tables? We  
can’t  
just go to config files or not DB models for reasons I won’t get into.

Thanks,  
Dave

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [February 10, 2006, 10:34pm UTC](https://www.ruby-forum.com/t/can-rails-cache-a-table/64846/2 "2006-02-10T22:34:07Z")

</div>

You could try loading the configuration globaly in the environment.rb  
file.  
Then use that throughout your program instead of calling off the model.  
David

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [February 8, 2007, 2:01pm UTC](https://www.ruby-forum.com/t/can-rails-cache-a-table/64846/3 "2007-02-08T14:01:35Z")

</div>

David Genord II wrote:

> You could try loading the configuration globaly in the environment.rb  
> file.  
> Then use that throughout your program instead of calling off the model.  
> David

So, if I have the table configurations like and a Model called  
Configuration, I could simply load the values from DB on environment.rb  
like:

config = Configuration.find(:first) #I’m only interested in firts row

the config model have a column named max\_posts\_per\_page

and them use the variable config in every part of my application, like

MyController …

def do\_some\_stuff  
@posts = Posts.display\_posts(config.max\_posts\_per\_page)  
end

will this work as this?

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [February 9, 2007, 9:17pm UTC](https://www.ruby-forum.com/t/can-rails-cache-a-table/64846/4 "2007-02-09T21:17:50Z")

</div>

Nice, but this is allways hiting my DB, how can I use is witout hitting  
my DB anytime I request the Configuration.max\_posts\_per\_page?

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [February 9, 2007, 1:43am UTC](https://www.ruby-forum.com/t/can-rails-cache-a-table/64846/5 "2007-02-09T01:43:15Z")

</div>

Hi David,  
You can do this with class variables. For example, in your  
Configuration model you can do  
@@max\_posts\_per\_page = Configuration.find(:first).max\_posts\_per\_page  
You can add a little function to wrap this if you like, like  
def self.max\_posts\_per\_page\_cached  
@@max\_posts\_per\_page  
end

Then in MyController you can do  
def do\_some\_stuff  
@posts = Posts.display\_posts(Configuration.max\_posts\_per\_page)  
end

J. mp wrote:

> David Genord II wrote:
> 
> > You could try loading the configuration globaly in the environment.rb  
> > file.  
> > Then use that throughout your program instead of calling off the model.  
> > David
> 
> So, if I have the table configurations like and a Model called  
> Configuration, I could simply load the values from DB on environment.rb  
> like:
> 
> config = Configuration.find(:first) #I’m only interested in firts row
> 
> the config model have a column named max\_posts\_per\_page
> 
> and them use the variable config in every part of my application, like
> 
> MyController …
> 
> def do\_some\_stuff  
> @posts = Posts.display\_posts(config.max\_posts\_per\_page)  
> end
> 
> will this work as this?

---

<div class="post-metadata">

**Author:** ![crucoba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e8c25b/32.png) [@crucoba](https://www.ruby-forum.com/u/crucoba)\
**Post date:** [February 9, 2007, 9:20pm UTC](https://www.ruby-forum.com/t/can-rails-cache-a-table/64846/6 "2007-02-09T21:20:23Z")

</div>

I found a soltution that works and don’t touch the DB, only load at  
application startup

In enviroment.rb

CONFIG = Configuration.find(:first) #find

in the application code:

class MyController \< ApplicationController

def index  
@ppp = CONFIG.posts\_per\_page

```
@ppp.freeze

#To change any vlue
CONFIG.posts_per_page = 22

CONFIG.save

```

end  
end
