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
,并且 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 bar
s, and the foo
has all the bar
s. Everything is fine.
Now I want to request some foo
s 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是你遇到了一个错误。我刚刚报告了附加示例的问题: 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
我认为您现在应该能够通过这样做获得正确的结果:
I think you should be able to get correct result by doing this for now: