加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Mongoid :: Errors :: MixedRelations save

发布时间:2020-12-17 03:22:06 所属栏目:百科 来源:网络整理
导读:我有3个型号: class Interest include Mongoid::Document has_many :user_hobbyendclass UserHobby include Mongoid::Document field :contacts,:type = Array belongs_to :interest,:foreign_key = "interest",inverse_of: nil belongs_to :interest,:fore
我有3个型号:

class Interest
  include Mongoid::Document

  has_many :user_hobby
end

class UserHobby
 include Mongoid::Document
  field :contacts,:type => Array

  belongs_to :interest,:foreign_key => "interest",inverse_of: nil
  belongs_to :interest,:foreign_key => "related_interests",inverse_of: nil
  embedded_in :user
end

class User
 include Mongoid::Document
 embeds_many :user_hobby
end

我必须在user和user_hobby之间添加嵌入式关系,但之后(在我的测试中)
当我想保存兴趣时,我有这个错误:

Mongoid::Errors::MixedRelations: Referencing a(n) UserHobby document from the Interest document via a relational association is not allowed since the UserHobby is embedded.

我看了这个话题但是Mongoid::Errors::MixedRelations也没帮我

谢谢.

解决方法

在这方面,主要问题是您的UserHobby模型嵌入在User中.根据mongodb,如果嵌入了文档,则除了嵌入文档之外的任何其他模型都不能引用它.如果要从其他模型引用UserHobby文档,则UserHobby和User不应该具有嵌入关系.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读