Forum: Ruby on Rails Can rails cache a table?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
David C. (Guest)
on 2006-02-09 01:40
(Received via mailing list)
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
David Genord II (Guest)
on 2006-02-10 23:34
(Received via mailing list)
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
J. m. (Guest)
on 2007-02-08 15:01
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?
jared (Guest)
on 2007-02-09 02:43
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?
J. m. (Guest)
on 2007-02-09 22:17

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?
J. m. (Guest)
on 2007-02-09 22:20
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
This topic is locked and can not be replied to.