【问题标题】:DataMapper Nested conditions: count issueDataMapper 嵌套条件:计数问题
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-09-17
        • 2011-06-13
        • 2021-08-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多