Why do my Rails initializers cache ENV variables in the console (Foreman, Dev env)

What started as a minor annoyance now turned into a headache. I am building a Rails 4 application and using Foreman for my dev installation with a Procfile and .env file for configuration. When I set the ENV variable in the .env file, it is correctly selected by my application. In this case, I set some ENV options for Paperclip in the initializer.

The problem arises when I go to change the value of ENV variables. In the console, if I type ENV ["MY_VAR"], it shows the new value. However, the value that was used in my initializer, which was supposedly launched when the console started, shows the old value! Nowhere in my project is the old value indicated anywhere. This makes me think that the environment is somehow cached or that the env variables are exported to my shell. I'm running out of places to watch, so any help would be greatly appreciated! I am developing on Mac (10.9.4) with Ruby 1.9.3-p374 and Rails 4.1.0.

Example:

ROOT / .env

S3_BUCKET=mybucket 

configurations / Initializers / paperclip.rb

 Paperclip::Attachment.default_options[:s3_credentials] = {bucket: ENV["S3_BUCKET"]} 

If I changed the value of S3_BUCKET to "newbucket" and ran "runman rails c" or "rails c" to enter the console, this is what happens:

 ENV["S3_BUCKET"] # => "newbucket" Paperclip::Attachment.default_options[:s3_credentials] # => {bucket: 'mybucket'} 

I should mention that this behavior also occurs in my classes that I injected into / lib. I think it's all because of something stupid that I forgot. Any ideas?

+8
ruby-on-rails environment-variables development-environment foreman
source share
1 answer

If you use Rails 4 out of the box, it comes with the Spring gem, which is designed to make your life easier by pre-loading an instance of your application in the background and reloading it as the code and configuration files change.

Spring, however, only controls the default Rails configuration files, so you need to configure Spring to monitor additional files that you want to cause a reboot.

Spring reads ~/.spring.rb and config/spring.rb for custom settings. You can add the following line to the file of your choice to see your .env file for changes:

 Spring.watch '.env' 

For more information, see the Spring configuration documentation in README.

+24
source share

All Articles