Re: preloading/caching small tables

Michael Schuerig
2005-07-28T20:42:59Z

On Thursday 28 July 2005 14:24, Imran Patel wrote:

> I have couple of small database tables that I would like to get
> pre-loaded (as hash[id] = value) "once" in my app at startup. I
would
> like to access the resulting hashes in my Controller logic. I have
> tried looking for answers on the wiki but haven''t been very
> successful. What would be the best way to do this?

You can just define a constant in the relevant classes

class Whatever
  ALL = find(:all)
end

If you need to access the objects by primary key, try this

class Whatever
  ALL = find(:all).inject({}) { |all, ar| all[ar.id] = ar.name; all }
end

My slightly overengineered version of the first example:
http://wiki.rubyonrails.com/rails/show/PreloadingReferenceData


Michael

--
Michael Schuerig                  Airtight arguments have
mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org        vacuous
conclusions.
http://www.schuerig.de/michael/   --A.O. Rorty, Explaining Emotions