« return

ActiveRecord ruby Script without Rails

23-Sep-2010, Ant

Below is an example script of how to use existing ActiveRecord Models in a rails app without loading the entire rails app.


#!/usr/bin/env ruby

require 'rubygems'
require 'active_record'
require 'yaml'
 
RAILS_ROOT = File.join(File.expand_path(File.dirname(__FILE__)), '..')
puts RAILS_ROOT
 
dbconfig = YAML::load(File.open("#{RAILS_ROOT}/config/database.yml"))
ActiveRecord::Base.establish_connection(dbconfig["development"])

require "#{RAILS_ROOT}/app/models/site"

Site.all.each do |site|
  puts site.domain
end