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
ご覧のとおり、マップに場所が埋め込まれ、プレーヤーが 1 つの場所を現在の場所として参照する単純なゲーム世界環境をモデル化しようとしています。
このアプローチを使用すると、Player クラスの「場所」属性を参照しようとすると、次のエラーが発生します。
Mongoid::Errors::DocumentNotFound: Document not found for class Location with id(s) xxxxxxxxxxxxxxxxxxx.
私の理解では、これは Location ドキュメントが埋め込まれているため、その埋め込みドキュメント (マップ) の範囲外を参照することが困難になっているためです。これは理にかなっていますが、埋め込みドキュメントへの直接参照をモデル化するにはどうすればよいでしょうか?