Hi, 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? Thanks imran -- "Oh, I used to be disgusted, now I try to be amused." - Elvis Costello
Imran Patel <imran.patel@...> writes:> > Hi, > > 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? > > Thanks > imranDon''t know if RoR has the equivalent of an Application object, but how about something in each model like: @@all_rows = self.find(:all) def all_rows @@all_rows end Then you could reference Model.all_rows and only take the hit to the database once.
Perhaps instead of using database tables, you could store the data with YAML instead. It sounds like it would be well-suited for how you want to use it. On 7/28/05, Imran Patel <imran.patel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > 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? > > Thanks > imran > -- > "Oh, I used to be disgusted, now I try to be amused." > - Elvis Costello > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
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