Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load environment variables in the Rails console?

I think this is a little, easy question!

I'm using .env file to keep all my environment variables, and i'm using foreman.

Unfortunately, these environment variables are not being loaded when running rails console rails c so, i'm now loading them manually after running the console, which is not the best way.

I'd like to know if there any better way for that.

like image 203
M.ElSaka Avatar asked Mar 12 '13 19:03

M.ElSaka


People also ask

How do I see environment variables in rails?

Use command ENV in rails console. That will return a hash of your environmental values you can access. Alternatively, you can access your environmental variables from your apps root path using the same command and the variables will be returned formatted.

How do I view environment variables in Ruby?

Ruby has direct access to environment variables via the ENV hash. Environment variables can be directly read or written to by using the index operator with a string argument. Note that writing to environment variables will only have an effect on child processes of the Ruby script.


1 Answers

About a year ago, the "run" command was added to foreman

ref: https://github.com/ddollar/foreman/pull/121

You can use it as follow:

foreman run rails console

or

foreman run rake db:migrate

like image 89
dquimper Avatar answered Oct 08 '22 16:10

dquimper