This is my old webpage. I'm keeping it online for archival reasons. My new webpage is ditrw.com
« Back

Active Record anywhere!

Published Aug 31 2008.Updated Nov 28 2009.

You can use ActiveRecord anywhere!

require 'rubygems'
require 'active_record'

ActiveRecord::Base.establish_connection({
  :adapter => 'postgresql',
  :user => 'foo',
  :password => 'bar',
  :database => 'whatever'
})

class Task < ActiveRecord::Base
  set_table_tame "a_legacy_thingie"
  
  def utility_methods
    update_attribute(:title, "yep")
  end
end

Task.find(:first)

Etcetera. It’s ActiveRecord, you know what to do. Going wild:

ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")

ActiveRecord::Schema.define do
  create_table :posts do |t|
    t.string :title
    t.text :excerpt, :body
  end
end

class Post < ActiveRecord::Base
  validates_presence_of :title
end

Post.create(:title => "A new post!")
Post.create(:title => "Another post", :excerpt => "The excerpt is an excerpt.")

puts Post.count
# => 2

Got a comment? Send me an e-mail! Or use Twitter.

>