Generating headless, console applications

You can make a rails app that never runs in a server, and then use pieces to do what you want. You can use script/runner to run methods in a model, or use code like this to run a controller:

#!/usr/bin/env ruby

require 'rubygems' require_gem 'activerecord' require_gem 'actionpack'

SCRIPT_PATH = File.expand_path(File.dirname(__FILE__))

$LOAD_PATH << "#{SCRIPT_PATH}/../app/models/" $LOAD_PATH << "#{SCRIPT_PATH}/../app/controllers/" $LOAD_PATH << "#{SCRIPT_PATH}/../lib/"

require 'application'

require "#{SCRIPT_PATH}/../config/environment.rb" require 'model' require 'controller'

Model.action SomeController.action

etc.

Make sure to set the rails_env environment variable: RAILS_ENV=production

I use script/runner where possible, but I have to use the above code sometimes, such as when a cron job effects caching.

Michael