Skip to content

Instantly share code, notes, and snippets.

@protocarl
Created January 8, 2010 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save protocarl/272379 to your computer and use it in GitHub Desktop.
Save protocarl/272379 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'dm-core'
gem 'dm-core', '0.10.2'
class Post
include DataMapper::Resource
# !> method redefined; discarding old one?
property :id, Serial
property :title, String
has n, :comments, :repository => :comments
end
class Comment
include DataMapper::Resource
def self.default_repository_name() :comments; end
# !> method redefined; discarding old first
property :id, Serial
property :body, Text
belongs_to :post, :repository => :default
end
DataMapper::Logger.new($stdout)
DataMapper.setup(:default, 'sqlite3::memory:')
DataMapper.setup(:comments, :adapter => :in_memory)
# This creates a comments table in the sqlite3 db
DataMapper.repository(:default).auto_migrate!
DataMapper.repository(:comments).auto_migrate!
# !> instance variable @field_types not initialized
post = Post.create(:title => 'Hai')
post.comments.create(:body => 'Hi')
# This should execute 1 query on each adapter
Post.first('comments.body' => 'Hi') # => nil
# >> ~ (0.000042) SELECT sqlite_version(*)
# >> ~ (0.000055) DROP TABLE IF EXISTS "posts"
# >> ~ (0.000015) DROP TABLE IF EXISTS "comments"
# >> ~ (0.000010) PRAGMA table_info("posts")
# >> ~ (0.000270) CREATE TABLE "posts" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "title" VARCHAR(50))
# >> ~ (0.000007) PRAGMA table_info("comments")
# >> ~ (0.000101) CREATE TABLE "comments" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "body" TEXT, "post_id" INTEGER NOT NULL)
# >> ~ (0.000090) CREATE INDEX "index_comments_post" ON "comments" ("post_id")
# >> ~ (0.000040) INSERT INTO "posts" ("title") VALUES ('Hai')
# >> ~ (0.000076) SELECT "posts"."id", "posts"."title" FROM "posts" INNER JOIN "comments" ON "posts"."id" = "comments"."post_id" WHERE "comments"."body" = 'Hi' GROUP BY "posts"."id", "posts"."title" ORDER BY "posts"."id" LIMIT 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment