Permalink
Browse files

Add ActiveRecord and Sequel examples.

  • Loading branch information...
1 parent 2e22411 commit 11c6a65ca5431114530d214765032eb54c330c64 @mooffie committed Apr 14, 2010
Showing with 79 additions and 0 deletions.
  1. +44 −0 examples/drx_activerecord.rb
  2. +35 −0 examples/drx_sequel.rb
@@ -0,0 +1,44 @@
+require 'rubygems'
+require 'activerecord'
+
+#
+# This is part of a blogging website. Users write posts. A post
+# belongs to a user.
+#
+
+ActiveRecord::Base.logger = Logger.new(STDERR)
+ActiveRecord::Base.colorize_logging = false
+
+ActiveRecord::Base.establish_connection(
+ :adapter => 'sqlite3',
+ :database => ':memory:'
+)
+
+ActiveRecord::Schema.define do
+ create_table :users do |table|
+ table.column :name, :string
+ table.column :mail, :string
+ end
+ create_table :posts do |table|
+ table.column :user_id, :integer
+ table.column :title, :string
+ table.column :body, :string
+ end
+end
+
+class User < ActiveRecord::Base
+ has_many :posts
+end
+
+class Post < ActiveRecord::Base
+ belongs_to :users
+end
+
+david = User.create(:name => 'David')
+david.posts.create(:title => 'Lies')
+david.posts.create(:title => 'Truths')
+
+post = User.find(1).posts.first
+
+require 'drx'
+post.see
View
@@ -0,0 +1,35 @@
+require 'rubygems'
+require 'sequel'
+
+#
+# This is part of a blogging website. Users write posts. A post
+# belongs to a user.
+#
+
+DB = Sequel.sqlite(':memory:')
+
+DB.create_table :posts do
+ primary_key :id
+ Integer :user_id
+ String :title
+ String :body
+end
+
+DB.create_table :users do
+ primary_key :id
+ String :name
+ String :email
+end
+
+class Post < Sequel::Model
+ many_to_one :user
+end
+
+class User < Sequel::Model
+ one_to_many :posts
+end
+
+post = Post.create(:title => 'Lies', :user => User.create(:name => 'David'))
+
+require 'drx'
+post.see

0 comments on commit 11c6a65

Please sign in to comment.