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).