I recently wanted to set up a quick daemon process that used ActiveRecord outside the rails framework. I also wanted it to use sqlite, just to keep install/dependencies simple. I found not so good documentation on how to do this... and after a few missteps it turns out it's not that hard. The only gem dependencies are activerecord and sqlite3-ruby. You'll also need sqlite working. Code follows
require 'rubygems'
require 'sqlite3'
require 'activerecord'
# connect to database. This will create one if it doesn't exist
MY_DB_NAME = ".my.db"
MY_DB = SQLite3::Database.new(MY_DB_NAME)
# get active record set up
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => MY_DB_NAME)
# create your AR class
class Update < ActiveRecord::Base
end
# do a quick pseudo migration. This should only get executed on the first run
if !Update.table_exists?
ActiveRecord::Base.connection.create_table(:updates) do |t|
t.column :account_name, :string
t.column :last_update_time, :timestamp
t.column :last_update_id, :integer
end
end
Continue reading Standalone ActiveRecord and SQLite3.
