【问题标题】:Find records without _type through mongoid - Single table inheritance通过mongoid查找没有_type的记录——单表继承
【发布时间】:2014-03-20 16:54:58
【问题描述】:

我使用 Ruby gem mongoid 并想查找不继承自任何东西的记录。

所以有 2 个模型。一个基类 Product 和一个继承自 Product 的类 Product::Translation。

如何找到不继承任何东西且没有 _type 字段的纯产品记录。

我尝试了这些查询但不起作用:

  • Product.where(:_type.exists => false)
  • Product.exists(_type: false)
  • Product.where(_type: nil)
  • Product.exists(_type: false)

【问题讨论】:

  • 关于inheritance_column 的内容。它等于“_type”。

标签: ruby-on-rails ruby mongodb mongoid


【解决方案1】:

Mongoid 足够聪明,可以在我们进行继承时应用默认 _type,即使对于基类也是如此。所以,

Product.where(_type: "Product") # will return all documents of type Product.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-13
    • 1970-01-01
    • 1970-01-01
    • 2013-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多