我有3个型号:
class Opportunity
include Mongoid::Document
...
...
has_many :proposals
end
class Proposal
include Mongoid::Document
...
...
belongs_to :opportunity
has_one :risk
scope :eager, ->{ includes([:opportunity, :risk]) }
def as_json(options={})
attrs = super(:include => [:opportunity, :risk])
attrs
end
end
class Risk
include Mongoid::Document
...
...
belongs_to :proposal
end
如果我现在尝试加载并渲染结果:
render json: {result: Proposal.eager.first()}
我收到包含机会的提案记录,但缺少风险记录。我做错了吗?
同时做Proposal.includes(:risk,:opportunity).first()不包括Risk的记录。我检查了数据库 - 记录在那里。当我查看risk_id字段时,它在那里有正确的ID。我以为我可以用has_one关系进行急切加载?
有什么想法吗?