I have some scripts that I need to run that I want to access the full environment from my rails app.
I know I’ve used script/runner before in Rails 2.3.
But I’ve also used ‘delay_job’ which loads the rails environment like this (2.3 code):
#!/usr/bin/env ruby
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment'))
require 'delayed/command'
The script I’m working on now pulls data off a message queue and then I want it to use active record and my models to insert that data into a logging database (that may or may not be the same as the DB that the rest of the app uses.
From your script, you need to require the file
config/environment.rbin your application. Note that this is exactly what DJ does here. This is true in Rails 3 as well.Note that if you turn your script into a Rake task (which you can stick in
Rakefileor in your own*.rakefile inlib/tasks), you can simply have your task depend on the Rails-defined taskenvironment.