ruby-on-rails – Mongoid ::错误:: MixedRelations
发布时间:2020-12-17 01:27:17 所属栏目:百科 来源:网络整理
导读:我有一个用户模型嵌入“ one to many”关注列表,如下所示: class User include Mongoid::Document field :uid field :name field :user_hash embeds_many :watchlistsendclass Watchlist include Mongoid::Document field :html_url field :description #fi
我有一个用户模型嵌入“
one to many”关注列表,如下所示:
class User include Mongoid::Document field :uid field :name field :user_hash embeds_many :watchlists end class Watchlist include Mongoid::Document field :html_url field :description #field :name field :fork_,:type => Boolean field :forks,:type => Integer field :watchers,:type => Integer field :created_at,:type => DateTime field :pushed_at,:type => DateTime field :avatar_url embedded_in :user has_and_belongs_to_many :tags end 监视列表还应引用many to many标签模型,反之亦然: class Tag include Mongoid::Document field :name,type: String has_and_belongs_to_many :watchlists end 无论如何,这导致了一个错误,似乎不可能出现这种“混合”关系: Mongoid::Errors::MixedRelations (Referencing a(n) Watchlist document from the Tag document via a relational association is not allowed since the Watchlist is embedded.): app/controllers/home_controller.rb:53:in `tagging' UPDATE UPDATE更新(坦克到durran支持) 你对如何克服这个问题有任何想法吗? 解决方法
在mongoid中,您不能引用嵌入式文档.所以问题在于你的标签模型中定义了habtm.您可以在嵌入式监视列表中使用HABTM,而没有任何反向关系.
class User include Mongoid::Document embeds_many :watchlists end class Watchlist include Mongoid::Document embedded_in :user has_and_belongs_to_many :tags,inverse_of: nil end class Tag include Mongoid::Document end 但是,如果必须在标签中引用关注列表,则可以手动维护两侧的ID数组,如Tyler已经指出的那样. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |