Skip to content

Instantly share code, notes, and snippets.

@jakalada
Created November 23, 2009 17:22
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/241210 to your computer and use it in GitHub Desktop.
Save jakalada/241210 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
post = Post.create
tag = Tag.create
post.add_tag tag
p post.tags #=> [#<Tag @values={:id=>1}>]
p tag.posts #=> [#<Post @values={:id=>1}>]
p Tagging.all #=> [#<Tagging @values={:id=>1, :post_id=>1, :tag_id=>1}>]
p Post.all #=> [#<Post @values={:id=>1}>]
p Tag.all #=> [#<Tag @values={:id=>1}>]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment