DataMapper 嵌套条件:计数问题

发布于 2024-11-05 15:52:10 字数 964 浏览 0 评论 0原文

这是模型:

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,并且 foo 拥有所有 >酒吧。一切都很好。

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

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

Here is the models:

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

Inserting some data:

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

So, now I have one foo, three bars, and the foo has all the bars. Everything is fine.

Now I want to request some foos having bar#1 and bar#3:

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

And here is the question: why array length is 1 and collection count is 2? How can I get both 1? I'd like to stick to the request with the nested conditions. Is it a bug or a misuse?

DM 1.1.0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜尕妞 2024-11-12 15:52:10

不幸的是你遇到了一个错误。我刚刚报告了附加示例的问题: https://github.com/datamapper/dm -聚合/问题/3

Unfortunately you've hit a bug. I just reported an issue with your example attached: https://github.com/datamapper/dm-aggregates/issues/3

蓬勃野心 2024-11-12 15:52:10

我认为您现在应该能够通过这样做获得正确的结果:

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

I think you should be able to get correct result by doing this for now:

Foo.count(Foo.bars.id => [1,3])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文