Mongoid 属性似乎不存在,但我可以通过它查询

Mongoid attribute seems to not be there, but I can query by it

以下是我可以用最简洁的方式展示的情况:

Model.where(:flag => false).count
=> 6
Model.where(:flag => false).first.flag
=> nil

一些背景:曾经在我的模型上有一个名为 flag 的属性字段。相反,我将其更改为 has_one flag 。我正在尝试 运行 迁移,除了出于某种原因有 6 条记录标记为 false,除非我自己找到它们,flag 为零。

我的假设是,当我实际上 运行 .flag 本身时,它会尝试查询 has_one 关系。但是当我 运行 a where 时,它实际上会查询仍然设置了 field :flag 的旧记录。我该如何解决这个问题?

我发现这样做的方法是通过 unset

这是我找到的一个小片段:

https://coderwall.com/p/wcx4pq/mongoid-remove-unnecessary-properties-from-documents