使用 Mongoid,假设我有以下类:
class Map
include Mongoid::Document
embeds_many :locations
end
class Location
include Mongoid::Document
field :x_coord, :type => Integer
field :y_coord, :type => Integer
embedded_in :map, :inverse_of => :locations
end
class Player
include Mongoid::Document
references_one :location
end
如您所见,我正在尝试对一个简单的游戏世界环境进行建模,其中地图嵌入了位置,玩家将单个位置引用为他们的当前位置。
使用这种方法,当我尝试引用 Player 类的“位置”属性时出现以下错误:
Mongoid::Errors::DocumentNotFound: Document not found for class Location with id(s) xxxxxxxxxxxxxxxxxxx.
我的理解是,这是因为位置文档是嵌入的,因此很难在其嵌入文档(地图)的范围之外进行引用。这是有道理的,但是我如何对嵌入文档的直接引用进行建模呢?