【发布时间】:2011-08-21 20:14:59
【问题描述】:
这里是模型:
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,三个bars,foo 拥有所有bars。一切都很好。
现在我想请求一些具有bar#1 和bar#3 的foos:
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
【问题讨论】:
标签: ruby nested associations datamapper