Where to keep static arrays of data

Hello,

I was wondering if there is a right way to keep some static arrays of data inside the application. I want to be able to access them globally. (it's not for setting vars, just simple arrays, i.e. ['ICQ', 'AIM', 'GTALK'] etc). Also I don't wan't to use a database for that, need a hardcoded option.

Read o the forum, that some people use environment.rb, but wasn't sure about that. I would like to have a separate file for each array of data, but what is the best practise for that? Just create rb files in a separate folder?

Hi,

bill walton wrote:

Hi,

Hello,

I was wondering if there is a right way to keep some static arrays of data inside the application.

I'd consider storing them in the models to which they relate. If they need their own models, that's easy too. The models don't have to inherit from ActiveRecord.

I'm about to try something kind of similar. I'm going to be experimenting with storing state and country info outside the DB, since it's static and relatively small. I hope to use one of the tableless AR libraries to make the data act like an AR model, though. Perhaps I'll find out why no one does this... :slight_smile:

HTH, Bill

Best,

bill walton wrote: > Hi, > >> Hello, >> >> I was wondering if there is a right way to keep some >> static arrays of data inside the application. > > I'd consider storing them in the models to which they relate. If they > need their own models, that's easy too. The models don't have to > inherit from ActiveRecord.

I'm about to try something kind of similar. I'm going to be experimenting with storing state and country info outside the DB, since it's static and relatively small. I hope to use one of the tableless AR libraries to make the data act like an AR model, though. Perhaps I'll find out why no one does this... :slight_smile:

Thanks for you replies.

I would be very grateful if you could give me an example of how to store it in the model... Should I create local collection, or define a method for it? Also how would I access this data from a controller?

Hi Zovar

     You can do it different ways.one method is create a file say global_data.rb in the folder config/initializers Then define these array there.You can also define also a module there like

in global_data.rb

module GlobalData    my_array = ['ICQ','AIM', 'GTALK'] end

     Now you can access this from anywhere in your application like GlobalData::my_array

       If you want you can freeze this also like

module GlobalData    my_array = ['ICQ','AIM', 'GTALK'] end GlobalData.freeze

Or if you want global variables(Please avoid its use as far as possible) you can do like inside this same file global_data.rb

$my_array = ['ICQ','AIM', 'GTALK']

      So from anywhere you can access it like $my_array

          You can also think of defining my_array above as constant if needed

Sijo

Some good solutions here already, but one that I didn't see mentioned was YAML. It's overkill for storing a small number of things, but can be handy if you need a big hash table.

Basically, you have a regular YAML file (call it config/ foo_lookup.yml, for instance) and then do this in a model:

class SomeModel < AR::Base   FOO_LOOKUP = YAML::load_file(File.join(RAILS_ROOT, 'config', 'foo_lookup.yml')) end

Then you can use it pretty much anywhere as SomeModel::FOO_LOOKUP. Note that since it runs at the class level, it will only get loaded once in production.

The other (small) nice thing with this setup is that it keeps changes to the constants apart from the model - handy if you're ever poring over source control logs...

--Matt Jones

Thanks a lot!

Hi,

Just a question regarding the following code

class SomeModel < AR::Base   FOO_LOOKUP = YAML::load_file(File.join(RAILS_ROOT, 'config', 'foo_lookup.yml')) end

Why should the model inherit from ActiveRecord::Base ? Would the array be loaded only once if such a class was in the lib (without AR inheritance) instead of app/models (which may require the inheritance) ?

Thanks for your help I'm quite interested in keeping static arrays like that in memory but having YAML files constantly being read and structures filled would ruin all targeted performance gain.