Skip to content

Instantly share code, notes, and snippets.

@slaskis
Created April 30, 2010 23:09
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 slaskis/bdc83b7c7f8797517f03 to your computer and use it in GitHub Desktop.
Save slaskis/bdc83b7c7f8797517f03 to your computer and use it in GitHub Desktop.
require "rubygems"
require "dm-core"
class Tag
include DataMapper::Resource
property :id, Serial
property :name, String
end
class Entry
include DataMapper::Resource
has n, :tags, :through => Resource
property :id, Serial
property :title, String, :length => 255, :index => true
end
DataMapper::Logger.new(STDOUT,:debug)
DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/bula.db")
DataMapper.auto_migrate!
a = Tag.create( :name => "A" )
b = Tag.create( :name => "B" )
Tag.create( :name => "C" )
Tag.create( :name => "D" )
e = Entry.create(:title => "Hello")
e.tags << a
e.tags << b
e.save
e = Entry.create(:title => "World")
e.tags.create( :name => "H" )
e.tags.create( :name => "I" )
e.tags << a
e.save
puts Entry.all( :tags => a ).size # => 1 (expected 2)
puts Entry.all( :tags => b ).size # => 1
# Workaround...
puts Entry.all.select {|e| e.tags.include? a }.size # => 2
puts Entry.all.select {|e| e.tags.include? b }.size # => 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment