Skip to content

Instantly share code, notes, and snippets.

@pjb3
Created June 30, 2010 02:09
Show Gist options
  • Select an option

  • Save pjb3/458124 to your computer and use it in GitHub Desktop.

Select an option

Save pjb3/458124 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'dm-core'
require 'dm-migrations'
DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, 'sqlite::memory:')
class AttachmentTag
include DataMapper::Resource
property :id, Serial
belongs_to :attachment
belongs_to :tag
end
class Attachment
include DataMapper::Resource
property :id, Serial
property :filename, String
has n, :attachment_tags
has n, :tags, :through => :attachment_tags
end
class Tag
include DataMapper::Resource
property :id, Serial
property :tagtext, String
has n, :attachment_tags
has n, :attachments, :through => :attachment_tags
end
DataMapper.auto_migrate!
10.times do |n|
Tag.create :tagtext => "tag##{n}"
end
10.times do |n|
a = Attachment.create :filename => "attachment##{n}"
Tag.all.each do |t|
AttachmentTag.create :attachment => a, :tag => t
end
end
Attachment.all.each do |a|
a.attachment_tags.each do |at|
puts "#{a.filename} is tagged with #{at.tag.tagtext}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment