· 8 years ago · Apr 09, 2018, 12:30 AM
1## post.rb
2class Post
3 include DataMapper::Resource
4 property :id , Integer , :serial => true
5 property :title , String , :length => 0..255
6 property :body , Text
7end
8
9## tag.rb
10
11class Tag
12 include DataMapper::Resource
13
14 property :id, Serial
15 property :name, String
16
17 belongs_to :post
18end
19
20## extend_post.rb
21
22class Post
23 has n, :tags
24end
25
26## test_post.rb
27
28require 'rubygems'
29require 'dm-core'
30
31DataMapper::Logger.new(STDOUT, :debug)
32DataMapper.setup(:default, 'sqlite3::memory:')
33
34require 'post'
35require 'tag'
36DataMapper.auto_migrate!
37
38# see, this comes post migration.
39require 'extend_post'
40
41@post = Post.create(:title => 'test')
42puts @post.tags
43
44## output [plain_text]
45Thu, 31 Jul 2008 09:36:58 GMT ~ debug ~ DROP TABLE IF EXISTS "posts"
46Thu, 31 Jul 2008 09:36:58 GMT ~ debug ~ PRAGMA table_info('posts')
47Thu, 31 Jul 2008 09:36:58 GMT ~ debug ~ SELECT sqlite_version(*)
48Thu, 31 Jul 2008 09:36:58 GMT ~ debug ~ CREATE TABLE "posts" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "title" VARCHAR(255), "body" TEXT)
49Thu, 31 Jul 2008 09:36:58 GMT ~ debug ~ DROP TABLE IF EXISTS "tags"
50Thu, 31 Jul 2008 09:36:58 GMT ~ debug ~ PRAGMA table_info('tags')
51Thu, 31 Jul 2008 09:36:58 GMT ~ debug ~ CREATE TABLE "tags" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "name" VARCHAR(50), "post_id" INTEGER)
52Thu, 31 Jul 2008 09:36:58 GMT ~ debug ~ INSERT INTO "posts" ("title") VALUES ('test')
53Thu, 31 Jul 2008 09:36:58 GMT ~ debug ~ SELECT "id", "name", "post_id" FROM "tags" WHERE "post_id" IN (1) ORDER BY "id"