Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Load the environment on require in Rails

  • Loading branch information...
commit b9f2810556d8b9daf1514895f59fdba2bdd67116 1 parent ed610ea
@bkeepers authored
Showing with 3 additions and 7 deletions.
  1. +3 −7 lib/dotenv/railtie.rb
10 lib/dotenv/railtie.rb
@@ -7,12 +7,8 @@ class Railtie < Rails::Railtie
task :dotenv do
Dotenv.load ".env.#{Rails.env}", '.env'
- task :environment => :dotenv
- end
- before_configuration 'dotenv', :group => :all do
- Dotenv.load ".env.#{Rails.env}", '.env'
+Dotenv.load ".env.#{Rails.env}", '.env'

3 comments on commit b9f2810


@bkeepers - Just noticed that the before_configuration call doesn't seem to resolve in rails, your new approach here seems like it should work but if you want to follow the load-it-during-initialization paradigm you've been doing I believe you can follow this guy's post:

Trying to figure out what the optimal spot would be, seems like sooner the better so maybe:

initializer 'dotenv', before: :load_environment_config do |app|
  Dotenv.load ".env.#{Rails.env}", '.env'

I could muck around with it a bit and send a pull request your way if you'd like.


Ok, PR opened here: #15


@bkeepers - Just had an idea, what if you made a separate file which instantly ran the load which you could require when you needed it? ie:

gem 'dotenv', require: 'dotenv/instant'

Or maybe the opposite...

gem 'dotenv', require: 'dotenv/before_configuration' #prevents the on-require load from executing, puts it into an initializer instead

Some might prefer a slightly more predictable loading pattern than immediately when the gem comes into memory.

Please sign in to comment.
Something went wrong with that request. Please try again.