Rails 2.3: Using ActiveRecord from within console application
If you use Ruby on Rails and want to have access to ActiveRecord and all the other nifty features from within a standalone Ruby script (for instance, a cronjob script that checks integrity of your data), all you need to do is including the “environment” script that fully sets up the Rails environment. In this case I use the “../config” path because my script is located in the app’s “scripts” directory.
#!/usr/bin/ruby
require ‘../config/environment’
# now all Rails features are available, all plugins loaded etc.
# demo
records = Model.find :all
for r in records
puts ‘Checking ’ + r.name + ’ …’
end