talk_groups表
field :parent_topic_id, type: Integer, default: -1
has_many :topics, :dependent => :destroy
belongs_to :parent_topic, :class_name => 'Topic', :foreign_key => :parent_topic_id主题表
belongs_to :talk_group上面的关系在sqlite/mysql上运行得很好,但在mongoid上就不起作用了,因为当一个模型不能有多个并且belongs_to与另一个相同的模型时。
parent_topic.talk_group将显示Mongoid::Errors::AmbiguousRelationship: error
发布于 2014-12-08 22:22:39
这个问题并不是因为“当一个模型不能有多个并且belongs_to与另一个相同的模型时”。
这是因为当mongoid查看topic模型时,它无法知道talk_group字段是否引用了talk_group模型中的topics或parent_topic关系。
mongoid提供了一个选项来避免这种"AmbiguousRelationship“它是inverse_of选项...
因此,为了解决这个问题,您应该将topic模型修改为
belongs_to :talk_group, inverse_of: :parent_topic并且talk_group模型应该变成
has_many :topics, dependent: :destroy
belongs_to :parent_topic, class_name: 'Topic'
# you can add inverse_of option on this side as well although it's not mandatory
belongs_to :parent_topic, class_name: 'Topic', inverse_of: :talk_group有关更多信息,请参阅this question。
https://stackoverflow.com/questions/27351164
复制相似问题