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.
Bd6167863690cb320d65bb9204619817?d=identicon&s=25 David Clements (Guest)
on 2006-02-09 00: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
95badc8378de83a449ff75027e67e9ed?d=identicon&s=25 David Genord II (Guest)
on 2006-02-10 22: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
A00673345921ae8c2e5570d1bd48b2e2?d=identicon&s=25 J. mp (lerias)
on 2007-02-08 14: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?
31162d0b45373bd0dd178c686e1df490?d=identicon&s=25 jared (Guest)
on 2007-02-09 01: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?
A00673345921ae8c2e5570d1bd48b2e2?d=identicon&s=25 J. mp (lerias)
on 2007-02-09 21: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?
A00673345921ae8c2e5570d1bd48b2e2?d=identicon&s=25 J. mp (lerias)
on 2007-02-09 21: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.