两个Mongoid文档模型,一个继承自另一个。 (1)当查询父模型的" _type"对于父类型的字段,返回的记录正确地返回匹配类型,但是,这些记录具有NO _type字段。
(2)因此,如果仅在没有_type字段约束的情况下再次查询父模型,则返回的记录将按预期包含父模型和子模型,并且这些记录具有_type字段。
(3)如果现在我们重复(1),那些记录将具有_type字段。你是否也经历过这种奇怪的现象?
示例:
# parent class
class Invoice
include Mongoid::Document
end
# child class
class Estimate < Invoice
include Mongoid::Document
end
rails c
# (1)
Invoice.where(_type: "Invoice").to_a.map(&:_type)
# NoMethodError: undefined method `_type' for #<Invoice:0x007ffeada82178>
# (2)
Invoice.all.to_a.map(&:_type)
# => ["Estimate", "Invoice", "Invoice"]
# (3)
Invoice.where(_type: "Invoice").to_a.map(&:_type)
# => ["Invoice", "Invoice"]
reload!
(1)
Invoice.where(_type: "Invoice").to_a.map(&:_type)
# NoMethodError: undefined method `_type' for #<Invoice:0x007ffeada82178>
MONGOID VERSION:5.0.2