Tuesday, February 26, 2008

The way to have global constants in rails application

I don't know if there is a better or formal way to keep global constants for rails application. If I want to the global constants to be used only by controllers and views, the best way is to declared them in application helper. If active record is also to use them, I have to put the definitions in environment.rb. Like the following:

# Global constant for this application
# I define an array here
SOME_GLOBAL_INSTANT = [100, 300, 500, 800, 1000]

I googled by "rails global constants" but get no better solutions. Anyone knows this please tell me.

Another way to load config into controllers is to use YAML file, check out this blog post:

http://snippets.dzone.com/posts/show/287

2 comments:

Scott said...

I used this method, it's clean and "OO".
http://toolmantim.com/articles/consolidating_your_apps_constants

An said...

I am considering using the approach from scott's link in the comment above (toolmantim.com), except use the Application_Helper module directly.