2

I have a static string array of Cities, States, Categories, etc that is accessed by my application in various places. Where should I put this? In a yml file someplace or a rb file in the lib directory?

Thanks!

Goalie
  • 3,045
  • 5
  • 35
  • 46

2 Answers2

4

See this so question

Basically, put constants in /config/initializers in a .rb file.


EDIT:

So this is not really constant data since "once a couple of months" the info will change. In that case you should put the info in the database and cache it. The caching will prevent round trips to the database and you can expire the cache when you need to update the info.

Community
  • 1
  • 1
Dty
  • 12,253
  • 6
  • 43
  • 61
  • Would static data lists of cities, states, and categories be considered constants? It seems like those are for one-off individual constants or setting constants. – Goalie Jul 12 '12 at 01:02
  • If your data will be changing then put them in the database. But a constant doesn't have to be just one value. It can be a list of things too, like cities and states. Plus, you're calling them "static" which implies they wont change...in other words, constant. – Dty Jul 12 '12 at 02:20
  • So they might change when I am adding a city or a category but those will be rare - once a couple of months. By putting them in the database I would need to make a trip to the db to get the information which I am also trying to avoid by going this route. – Goalie Jul 12 '12 at 03:02
  • You should put them in the database then cache it. That way you can change it later AND you get the performance benefit of not hitting the database. I'm adding this info to my answer. – Dty Jul 12 '12 at 03:14
0

I advise you to put this in config/locales/*.yml files. It's good because you can specify different city names for different languages. See internationalization guide for more information.

Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127