Skip to content

Instantly share code, notes, and snippets.

@bmarini
Created November 30, 2010 23:20
Show Gist options
  • Save bmarini/722621 to your computer and use it in GitHub Desktop.
Save bmarini/722621 to your computer and use it in GitHub Desktop.
Simple usage of AR outside of rails
require 'rubygems'
require 'active_record'
ActiveRecord::Base.establish_connection :adapter => "sqlite3", :database => ":memory:"
ActiveRecord::Schema.define do
create_table :authors do |t|
t.string :name, :null => false
end
add_index :authors, :name, :unique
create_table :posts do |t|
t.integer :author_id, :null => false
t.string :subject
t.text :body
t.boolean :private, :default => false
end
add_index :posts, :author_id
end
class Author < ActiveRecord::Base
has_many :posts
end
class Post < ActiveRecord::Base
end
author = Author.create :name => "Jeremy"
author.posts.build :subject => "A test", :body => "The body"
p author.posts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment