mysql - How to load configuration variables in rails via yaml files -
i trying use environment variable in config/database.yml.
this local_env.yml:
# local environment variables mysql_sock: "/tmp/mysql.sock" then check if correct rails console , sure enough:
>> irb(main):002:0> env["mysql_sock"] => "/tmp/mysql.sock" however in config/database.yml:
default: &default adapter: mysql2 socket: env["mysql_sock"] i following error:
mysql2::error: can't connect local mysql server through socket 'env["mysql_sock"]' (2) but...
default: &default adapter: mysql2 socket: "/tmp/mysql.sock" works!
what going wrong?
this practise followed defining configuration variables app ids, secret keys, s3 credentials etc , file put in .gitignore.
create yml file settings.yml (generally) or local_env.yml. define keys according environment.
development: mysql_sock: "/tmp/mysql.sock" production: mysql_sock: "file path here" and load via initializers. create file name "0_load_config.rb" inside initializers directory. '0' added in filename because file should loaded first other initializers.
then load local_env.yml file in constant
local_env = yaml.load_file("#{rails.root}/config/local_env.yml")[rails.env] then can access variable in application. can check via rails console also:
local_env["mysql_sock"] it give vaue had provided in local_env.yml i.e "/tmp/mysql.sock".
Comments
Post a Comment