34

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.

Flip
  • 6,233
  • 7
  • 46
  • 75
M.ElSaka
  • 1,264
  • 13
  • 20

3 Answers3

68

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

dquimper
  • 916
  • 7
  • 7
2

rails does not know about the environmental variables specified in .env file as it is specific to foreman. You need to set the environment explicitly before invoking rails console. Have a look at this question.

Community
  • 1
  • 1
Salil
  • 9,534
  • 9
  • 42
  • 56
1

I personnaly use dotenv in development and testing environements. With this approach, you don't have to prefix your commands, just call the initializer in your config/application.rb :

Bundler.require(*Rails.groups)

Dotenv::Railtie.load

HOSTNAME = ENV['HOSTNAME']
Ghis
  • 845
  • 10
  • 16