Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment variables cached in Rails config?

This behavior is really confusing me. It seems like the content of my ENV or my configuration is cached somewhere. Here's how to reproduce it:

In a fresh app (I'm using Ruby 2.0.0 and Rails 4.2.1), edit application.rb:

$ cat config/application.rb 
require File.expand_path('../boot', __FILE__)

require 'rails/all'

Bundler.require(*Rails.groups)

module Myapp
  class Application < Rails::Application
    config.active_record.raise_in_transactional_callbacks = true

    config.env_foo = ENV['FOO']
  end
end

The configuration item env_foo is now nil:

$ unset FOO  # make sure FOO is unset
$ rails console
Loading development environment (Rails 4.2.1)
2.0.0-p598 :001 > Rails.application.config.env_foo
 => nil

Set some environment variables and see what happens:

$ export FOO=barbapapa
$ rails console
Loading development environment (Rails 4.2.1)
2.0.0-p598 :001 > Rails.application.config.env_foo
 => nil 
2.0.0-p598 :002 > ENV['FOO']
 => "barbapapa" 

So the cache item is still nil but ENV has changed. Even if I change environment to production:

$ RAILS_ENV=production rails console
Loading production environment (Rails 4.2.1)
2.0.0-p598 :001 > Rails.application.config.env_foo
 => nil

Where is this configuration cached and how do I make it reflect the new ENV?

Note: I know that there are other ways to configure Rails, but I'm using Heroku so I think using the environment for configuration is encouraged.

like image 715
André Laszlo Avatar asked Mar 26 '15 22:03

André Laszlo


People also ask

Where is Rails cache stored?

Rails (as of 2.1) provides different stores for the cached data created by action and fragment caches. Page caches are always stored on disk. Rails 2.1 and above provide ActiveSupport::Cache::Store which can be used to cache strings.

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.

What are view cache in Rails?

1.1 Page Caching Page caching is a Rails mechanism which allows the request for a generated page to be fulfilled by the web server (i.e. Apache or NGINX) without having to go through the entire Rails stack. While this is super fast it can't be applied to every situation (such as pages that need authentication).


1 Answers

The Spring gem is preloading apps by default in the development environment in Rails 4.

To reload your app, simply kill the spring processes for the current project:

spring stop

Spring doesn't seem to be able to refresh on environment changes, but it does monitor files. If you are using an .env file, with dotenv for example, you can add it to config/spring.rb:

Spring.watch '.env'
like image 55
André Laszlo Avatar answered Sep 23 '22 07:09

André Laszlo