2011-05-09 82 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])