Undistraction
Undistraction

Reputation: 43401

Config variables available in rails environment file

I'm currently using an initializer to load a config.yml file into an AppConfig hash which offers access to variables for the environment. For production I am using environmental variables set on the server. I am using the following code to fallback to the config variable if the environmental variables are not set (i.e in development and test).

ENV['FACEBOOK_API_KEY'] || AppConfig['facebook_api_key']

My problem is that I need some of these variables to be available in the environment-specific file (development.rb/production.rb etc), but this file is loaded before the initialzers. How should I deal with this?

Upvotes: 3

Views: 2603

Answers (2)

roo
roo

Reputation: 7196

Have a look at the Rails guide for Configuration Initialization Events. There are events that you can hook into when doing this kind of configuration.

In short you can have configuration for the environment done after initialisation with:

#config/environments/development.rb
YourApp::Application.configure do
  config.after_initialize do
    #do some configuration after all initialisers have run
  end
end

Upvotes: 8

tadman
tadman

Reputation: 211740

If there's a way you can create a two-tier structure like database.yml you could always define separate configurations for each environment in the same file, then reference the appropriate version:

ENV['FACEBOOK_API_KEY'] || AppConfig[Rails.env] && AppConfig[Rails.env]['facebook_api_key']

Upvotes: 0

Related Questions