我正在尝试跟踪用户当前、以前和选择的(期末更改)订阅状态。
在每月周期结束时,如果chosen_subscription != current_subscription
,则current_subscription
更改。
class User
include Mongoid::Document
embeds_one :previous_subscription, class_name: "Subscription"
embeds_one :current_subscription, class_name: "Subscription"
embeds_one :chosen_subscription, class_name: "Subscription"
end
class Subscription
include Mongoid::Document
embedded_in :user
field :plan, type: String
field :credits, type: Integer
field :price_per_credit, type: BigDecimal
field :start, type: Date
field :end, type: Date
end
Mongoid 希望我以一种对我来说没有意义的方式详细说明这一点:
Mongoid::Errors::AmbiguousRelationship:
Problem:
Ambiguous relations :previous_subscription, :current_subscription, :chosen_subscription defined on User.
Summary:
When Mongoid attempts to set an inverse document of a relation in memory, it needs to know which relation it belongs to. When setting :user, Mongoid looked on the class Subscription for a matching relation, but multiples were found that could potentially match: :previous_subscription, :current_subscription, :chosen_subscription.
Resolution:
On the :user relation on Subscription you must add an :inverse_of option to specify the exact relationship on User that is the opposite of :user.
当我覆盖现有的 current_subscription 时会发生这种情况。我想,此时 Mongoid 想注销旧订阅的用户订阅。
当然,每个订阅对象只属于一个用户,并且user == user.previous_subscription.user == user.current_subscription.user == user.chosen_subscription.user
Subscription
然而,说这user
与三者中的任何一个相反,对我来说是没有意义的。
我应该如何正确构建它?