Created
February 16, 2009 17:52
-
-
Save snusnu/65269 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 | |
require 'rubygems' | |
gem 'dm-core', '=0.9.10' | |
require 'dm-core' | |
# uncomment for logging output | |
#DataMapper::Logger.new(STDOUT, :debug) | |
DataMapper.setup(:default, 'sqlite3::memory:') | |
class Branch | |
include DataMapper::Resource | |
property :id, Serial | |
property :name, String | |
has n, :branch_items | |
has n, :items, :through => :branch_items | |
has n, :featured_branch_items, | |
:class_name => 'BranchItem', | |
:child_key => [ :branch_id ], | |
:featured => true | |
# i would expect this to be scoped to | |
# only featured branch_items | |
has n, :featured_items, | |
:through => :featured_branch_items, | |
:class_name => 'Item', | |
:child_key => [ :branch_id ], | |
:remote_name => :item | |
# i also tried: (uncomment to try for yourself) | |
# 'branch_items.featured' => true | |
# => Unknown property 'branch_items' (ArgumentError) | |
# :featured => true | |
# => Unknown property 'featured' (ArgumentError) | |
end | |
class Item | |
include DataMapper::Resource | |
property :id, Serial | |
property :name, String | |
end | |
class BranchItem | |
include DataMapper::Resource | |
property :id, Serial | |
property :branch_id, Integer, :nullable => false | |
property :item_id, Integer, :nullable => false | |
property :featured, Boolean, :default => false | |
belongs_to :branch | |
belongs_to :item | |
end | |
DataMapper.auto_migrate! | |
b = Branch.create(:name => 'foo_branch') | |
i01 = Item.create(:name => 'foo_item') | |
i02 = Item.create(:name => 'bar_item') | |
BranchItem.create(:branch => b, :item => i01, :featured => true) | |
BranchItem.create(:branch => b, :item => i02) | |
b = Branch.get(1) | |
puts "b.items.size: #{b.items.size}" # correctly prints "2" | |
puts "b.featured_items.size: #{b.featured_items.size}" # incorrectly prints "2" | |
puts "b.featured_branch_items.size: #{b.featured_branch_items.size}" # correctly prints "1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment