Trevor and I were tasked with getting another ad-hoc report out the door today, and so I introduced Trevor to the concepts behind my previous post. The end result was a nice tidy source controlled Ruby script that would hit a live Rails app’s database. All well and good.
Then Trevor came up with a suggestion that in my blindness to solve the problem hadn’t occurred to me before. Why don’t we just include the appropriate model files, rather than recreating the models in the script?
He’s absolutely right. In fact, we can move all the Rails environment bumpf out into a separate file as well and just have every script that we develop require that file.
So, now our reporting scripts have a line at the top that reads
load 'script/ad_hoc_reports/include_me.rb'
…and include_me.rb looks a lot like this
ENV['RAILS_ENV'] = ARGV.first || ENV['RAILS_ENV'] || 'development'
require File.dirname(__FILE__) + '/../../config/boot'
require "#{RAILS_ROOT}/config/environment"
require 'user.rb'
# all your other model requires go here
So, in just one line of code now we can ‘require’ everything any console based reporting script would ever need.
Neat. Now I can get back to dealing with strange PayPerPost puzzle issues relating to ‘hp camera’, like this one

Sneaky man
Comment by Jules — January 8, 2007 @ 12:40 pm |
yea, really really sneaky!
now – wth does it mean?? ROFL
Comment by DonnaLeigh — January 8, 2007 @ 1:05 pm |
Hum I wanted to PPP but it doesn’t fit my blog. Like yours, though.
Comment by shelbycockrell — January 8, 2007 @ 2:24 pm |
Thank you.
Comment by Karen Moser — January 8, 2007 @ 2:28 pm |
Would script/runner save you some work?
Comment by Nathaniel Talbott — January 9, 2007 @ 10:13 am |
You’ve got me there Nathaniel – I’ve never used it. I’ll look into it and get back to you.
Comment by pwrighta — January 10, 2007 @ 6:34 am |
[...] More fun with Ruby reporting [...]
Pingback by Using ActiveRecord outside Rails — January 18, 2007 @ 10:42 am |