Created
November 23, 2009 17:00
-
-
Save jakalada/241185 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby19 | |
require 'sequel' | |
Sequel::Model.plugin :schema | |
DB = Sequel.sqlite | |
class Post < Sequel::Model | |
set_schema do | |
primary_key :id | |
end | |
many_to_many :tags, :join_table => :taggings | |
create_table unless table_exists? | |
end | |
class Tag < Sequel::Model | |
set_schema do | |
primary_key :id | |
end | |
many_to_many :posts, :join_table => :taggings | |
create_table unless table_exists? | |
end | |
class Tagging < Sequel::Model | |
set_schema do | |
primary_key :id | |
foreign_key :post_id, :table => :posts | |
foreign_key :tag_id, :table => :tags | |
end | |
many_to_one :posts | |
many_to_one :tags | |
create_table unless table_exists? | |
end | |
p DB.tables #=> [:posts, :tags, :taggings] | |
Post.drop_table | |
p DB.tables #=> [:tags, :taggings] | |
Tag.drop_table | |
p DB.tables #=> [:taggings] | |
Tagging.drop_table | |
p DB.tables #=> [] | |
Post.create_table | |
Tag.create_table | |
Tagging.create_table | |
p DB.tables #=> [:posts, :tags, :taggings] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment