2011-05-09 81 views
0

这里是机型:DataMapper的嵌套条件:计数问题

class Foo 
    include DataMapper::Resource 
    property :id, Serial 
    has n, :foo_bars 
    has n, :bars, :through => :foo_bars 
end 

class Bar 
    include DataMapper::Resource 
    property :id, Serial 
    has n, :foo_bars 
    has n, :foos, :through => :foo_bars 
end 

class FooBar 
    include DataMapper::Resource 

    belongs_to :foo, :key => true 
    belongs_to :bar, :key => true 
end 

插入一些数据:

f = Foo.create 
b1 = Bar.create 
b2 = Bar.create 
b3 = Bar.create 
f.bars = [b1, b2, b3] 
f.save 

所以,现在我有一个foo,三个bar S和foo拥有所有bar s。一切安好。

现在我想请一些foo小号为bar#1和#bar 3:

Foo.all(Foo.bars.id => [1,3]) 
=> [#<Foo @id=1>] #ok 
Foo.all(Foo.bars.id => [1,3]).count 
=> 2 #why? 

这里是问题:为什么数组的长度为1,集合数为2?我怎样才能得到1?我想坚持嵌套条件的请求。这是一个错误还是滥用?

DM 1.1.0

回答

-1

我想你应该能够这样做,现在得到正确的结果:

Foo.count(Foo.bars.id => [1,3])