Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to store (structured) configuration data in Rails

For the Rails 3 application I'm writing, I am considering reading some of the configuration data from XML, YAML or JSON files on the local filesystem.

The point is: where should I put those files? Is there any default location in Rails apps where to store this kind of content?

As a side note, my app is deployed on Heroku.

like image 719
abahgat Avatar asked Mar 01 '11 23:03

abahgat


2 Answers

What I always do is:

  • If the file is a general configuration file: I create a YAML file in the directory /config with one upper class key per environment
  • If I have a file for each environment (big project): I create one YAML per environment and store them in /config/environments/

Then I create an initializer where I load the YAML, I symbolize the keys of the config hash and assign it to a constant like APP_CONFIG

like image 193
Fernando Diaz Garrido Avatar answered Nov 02 '22 23:11

Fernando Diaz Garrido


I will usually adopt this method :

a config/config.yml

development:
  another_key: "test"
  app_name: "My App"
test:
  another_key: "test"
production:
  prova: "ciao"

then create a ostruct in a initializer

#config/initializer/load_config.rb
require 'ostruct'
config = OpenStruct.new(YAML.load_file("#{RAILS_ROOT}/config/config.yml"))
::AppSetting = OpenStruct.new(config.send(RAILS_ENV))

No DB table, per environment setup and you could retrive info in a simple way

AppSetting.another_key
AppSetting.app_name

here a reference
have a nice day!

like image 26
andrea Avatar answered Nov 03 '22 01:11

andrea