DataMapper嵌套条件:计数问题

时间:2011-05-09 16:38:04

标签: ruby nested associations 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,三个barfoo拥有所有bar个。一切都很好。

现在我想请求foo#1和bar#3的一些bar

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

2 个答案:

答案 0 :(得分:1)

不幸的是你遇到了一个bug。我刚刚报告了您的示例所附的问题:https://github.com/datamapper/dm-aggregates/issues/3

答案 1 :(得分:-1)

我认为你现在应该能够得到正确的结果:

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