User、Product、および Purchase の 3 つのモデル クラスがあります。Purchase は、注文する製品の数量を記述します。したがって、購入には 1 つの製品があり、製品は複数の購入に属する場合があります。しかし、問題に関する Mongoid メッセージ:
Mongoid::Errors::InverseNotFound (
Problem:
When adding a(n) Product to Purchase#product, Mongoid could not determine the inverse foreign key to set. The attempted key was 'purchase_id'.
Summary:
When adding a document to a relation, Mongoid attempts to link the newly added document to the base of the relation in memory, as well as set the foreign key to link them on the database side. In this case Mongoid could not determine what the inverse foreign key was.
Resolution:
If an inverse is not required, like a belongs_to or has_and_belongs_to_many, ensure that :inverse_of => nil is set on the relation. If the inverse is needed, most likely the inverse cannot be figured out from the names of the relations and you will need to explicitly tell Mongoid on the relation what the inverse is.
Example:
class Lush
include Mongoid::Document
has_one :whiskey, class_name: "Drink", inverse_of: :alcoholic
end
class Drink
include Mongoid::Document
belongs_to :alcoholic, class_name: "Lush", inverse_of: :whiskey
end):
しかし、モンゴイドの例は私の場合をカバーしていません。
これは私のモデルです:
class Purchase
include Mongoid::Document
field :quantity, text: String
has_one :product
belongs_to :user
end
class Product
include Mongoid::Document
belongs_to :purchases
end
class User
include Mongoid::Document
has_many :purchases
end
関係を正しく説明するにはどうすればよいですか?