Created
March 24, 2011 11:28
-
-
Save solnic/884915 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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