Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Loading rails environment in a script
#!/usr/bin/env ruby
# Load Rails
ENV['RAILS_ENV'] = ARGV[0] || 'production'
DIR = File.dirname(__FILE__)
require DIR + '/../config/environment'
# ... do stuff that requires using your rails models
@abachman

This comment has been minimized.

Copy link

abachman commented Aug 12, 2010

#!/usr/bin/env ruby
require File.expand_path('../../config/boot',  __FILE__)

... is easier. It's the preamble to most of the default script/_____ scripts in Rails < 3.

config/boot.rb handles the environment loading and the gem / vendor dance.

@webandy

This comment has been minimized.

Copy link

webandy commented Aug 12, 2010

Is your script completely outside the Rails app? If it is a Rake task you can load the environment to work with models by doing this: http://pastie.org/1088485

@jkriss

This comment has been minimized.

Copy link

jkriss commented Aug 12, 2010

If you use the regular environment variable convention, you don't even need that much.

require 'config/environment'
puts Rails.env

...gives you...

$ ruby test_rails.rb 
development
$ RAILS_ENV=production ruby test_rails.rb 
production
@zachwaugh

This comment has been minimized.

Copy link
Owner Author

zachwaugh commented Aug 12, 2010

Thanks for the tips, seems like there are a few ways to do it, but no one "right way". The use case for this is a script I just need to run once to do some maintenance or a script run regularly via cron. I'm thinking a rake task is probably the most appropriate place for this.

@kenrett

This comment has been minimized.

Copy link

kenrett commented Dec 14, 2013

You just saved our lives..! Thank you so much for this... Had nearly pulled all of my hair out!

@russellsilva

This comment has been minimized.

Copy link

russellsilva commented Jan 14, 2014

Is this current for Rails 3/4? It's my top Google result for "rails script load environment" so it might be worth updating.

@lloeki

This comment has been minimized.

Copy link

lloeki commented Mar 28, 2014

#!/usr/bin/env ruby
require File.expand_path('../config/environment',  __FILE__)

worked for me on Rails 3 (one-shot script located in Rails.root)

@dfwmountainbiker

This comment has been minimized.

Copy link

dfwmountainbiker commented Jun 18, 2014

works on Rails 4 for me, but only when i run from the main Rails root. I'm sure it's based on a basic tweak I could make to have it run in a subdir, but i'm lazy today.

@rhacker

This comment has been minimized.

Copy link

rhacker commented Sep 20, 2014

In Rails 4 or 3, you can try to copy the config from the file test/test_helper.rb and change the path then everything should work correctly.

@krongk

This comment has been minimized.

Copy link

krongk commented Mar 5, 2015

cool~

@taimoor

This comment has been minimized.

Copy link

taimoor commented Jun 18, 2015

@lloeki your solution worked for me in rails 3.

@giddie

This comment has been minimized.

Copy link

giddie commented Feb 22, 2016

FYI, I think that should be:

#!/usr/bin/env ruby

require File.expand_path('../config/environment', File.dirname(__FILE__))
# or
require "#{File.dirname(__FILE__)}/../config/environment"

Otherwise, the script will not work in the script subdirectory.

@localhostdotdev

This comment has been minimized.

Copy link

localhostdotdev commented Feb 4, 2019

👍 require File.expand_path('../config/environment', File.dirname(__FILE__)) works (Rails 5.2)

@localhostdotdev

This comment has been minimized.

Copy link

localhostdotdev commented Apr 9, 2019

even better: require_relative '../config/environment'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.