我正在尝试使用 RSpec 断言给定的 Mongoid 查询不会加载记录,只需检查它的存在,因为记录很大(几 MB),并且代码只需要知道记录是否存在.
我一直在玩.exists?
关联,但由于某种原因,这似乎不起作用,has_one
例如:
class Profile
include Mongoid::Document
has_one :chart
end
class Chart # this is heavy
include Mongoid::Document
belongs_to :profile
end
profile.chart.exists? # fails if chart returns nil
exists?
代理方法显然不适用于has_one
关系;尽管它记录在has_many
. 我想自己制作,但我需要在 RSpec 中测试确实只查询而不加载记录。我正在考虑做一些事情,比如测试生成给 Mongo 驱动程序的底层查询,就像你可以用.to_sql
. 有没有对应的Mongoid方法?