Skip to content

Instantly share code, notes, and snippets.

@solnic
Created March 24, 2011 11:28
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 solnic/884915 to your computer and use it in GitHub Desktop.
Save solnic/884915 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#
# encoding: utf-8
require 'dm-migrations'
require 'dm-validations'
DataMapper::Logger.new $stdout, :debug
DataMapper.setup :default, "sqlite::memory:"
class Tag
include DataMapper::Resource
property :id, Serial
property :name, String
has n, :entries, :through => Resource
end
class Entry
include DataMapper::Resource
property :id, Serial
property :title, String, :length => 255, :index => true
has n, :entry_tags
has n, :tags, :through => Resource
end
DataMapper.finalize
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
# This works:
puts a.entries.size # => 2
puts b.entries.size # => 1
# This blows up now:
puts Entry.all(:tags => a).size # => 2
# ^^^^^^^^^^^
# Tries to run this query: SELECT "id", "title" FROM "entries" WHERE "tag_id" = 1 ORDER BY "id""
puts Entry.all(:tags => b).size # => 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment