Skip to content

Instantly share code, notes, and snippets.

@jakalada
Created November 23, 2009 17:00
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 jakalada/241185 to your computer and use it in GitHub Desktop.
Save jakalada/241185 to your computer and use it in GitHub Desktop.
#!/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