34

Possible Duplicate:
Ruby on Rails: Where to define global constants?

I am interested in doing this the "Rails Way" on a new application. I would also like to refer to constants in some sort of context to make the code more readable. I have an application where a user can request access to another users's data set. This AccessRequest can have one of the following statuses:

Review Denied Approved

I can see these values being used in reporting features in the future, so I want to make them constants in order to avoid any spelling or capitalization issues. I thought I would just put these in a constants.rb file in the config/initializers directory.

I would like to refer to these as AccessRequest::REVIEW. Since I already have a model called AccessRequest, does it make sense to put them there? Or wrap them in a class in a constants.rb file in the config/initializers directory? Which way is the Rails Way?

Community
  • 1
  • 1
RubyRedGrapefruit
  • 12,066
  • 16
  • 92
  • 193

2 Answers2

43

You don't need to use constants in Rails 3.It is better to use the Rails::Application singleton.

In your application.rb you can define your constante like:

module Yourapp
  class Application < Rails::Application

    config.access_request.review = 'xxx'
  end
end

After in your code you can call

Yourapp::Application.config.access_request.review

After if you change value in each environment, You just define the config.xx in your config/environments/production.rb or other environment.

David J.
  • 31,569
  • 22
  • 122
  • 174
shingara
  • 46,608
  • 11
  • 99
  • 105
28

Belated reply, but posting as this answer still comes up in search results. Putting the constant in the model makes sense as the constant pertains directly to it. Using the Rails application config to store constants is incorrect.

As per the comment listed in application.rb:

# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded

This is still valid as of Rails 3.

Sasha
  • 544
  • 7
  • 5
  • 1
    That's absolutely correct Sasha, thanks for pointing that out. This has become a popular question. I've used the initializers directory to great success in my last two projects. It is very handy when you have some deployment-sensitive constants that need to be kept separate and deployed with recipes. – RubyRedGrapefruit Apr 28 '11 at 12:33
  • 6
    @AKWF When using initializers in this way, which scope do you use for defining constants? For example, say our initializer lives in `config/initializers/constants.rb`. Would you put `MAGIC_NUMBER = 42` right in the global scope, wrap it with a `module YourApp` block, or something else? – evanrmurphy Mar 06 '12 at 20:29
  • How do you make the constants different for your different environments? I'm looking for environment specific constants that I can access from my ERB template. – Bradley Jul 13 '12 at 19:42
  • @Bradley For environment specific constants, I believe [YAML config is preferred](http://railscasts.com/episodes/85-yaml-configuration-revised). – LessQuesar Jun 26 '13 at 04:01