Take me home

Active Record anywhere!

Published August 31, 2008

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

Questions or comments?

Feel free to contact me on Twitter, @augustl, or e-mail me at august@augustl.com.