Loading

Rails 3 Command Line Scripts

In Rails 2.x, you could do the following to load the entire stack into a command line script:

ENV["RAILS_ENV"] ||= "development"
require File.dirname(__FILE__) + "/../../config/boot"
require RAILS_ROOT + '/config/environment'

With Rails 3, this broke. It took a while, but I finally figured out how to get it working:

APP_PATH = File.expand_path('../../config/application',  __FILE__)
require File.expand_path('../../config/boot',  __FILE__)
ENV["RAILS_ENV"] ||= "development"
require APP_PATH
Rails.application.require_environment!

The rails runner command claims you can do

#!/usr/bin/env /path/to/your/app/script/rails

but it is broken (lighthouse ticket).


Related Posts

  1. Fix for Spork Not Reloading Models with Rails 3
  2. Getting Rails 2 Release Candidate To Work
  3. High Availability on a Shoestring -- PgPool-II Installation Woes
  4. Feedtools Database Cache
  5. SPF Records with Rails and ActionMailer
You should follow me on Twitter: @patrickxb