Can rails cache a table?


#1

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


#2

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


#3

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?


#4

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?


#5

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?


#6

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