Organizational Research By

Surprising Reserch Topic


mongoiderrorsmixedrelations  using -'ruby-on-rails,mongodb,mongoid'

I've a User model embedding "one to many" Watchlists like the following :

class User
  include Mongoid::Document

  field :uid
  field :name     
  field :user_hash

  embeds_many :watchlists

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

The Watchlist should also references a many to many Tag model and vice versa :

class Tag
  include Mongoid::Document
  field :name, type: String

  has_and_belongs_to_many :watchlists

Anyway, that's causing an error and seems that kind of "mixed" relation is not possible :

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'

Please note that watchlist, has to be dropped (user.watchlists.clear) than re-created (user.watchlists.find_or_create_by) four times a day, while Tag/s have to be persistent, relating the same embedded watchlists as before ( ... I'm not sure that is possible anyway, because of previous drop/creation ).

UPDATE of UPDATE ( tanks to durran support )
No, that's not possible: If you clear the embedded docs, the ids are gone as well, and new ones will get generate each time you create a new one.

Do you have any idea on how to overcome that ?
Is it better to split all three models in referenced relations ( three different collections )?

asked Oct 13, 2015 by patelnikul321
0 votes

Related Hot Questions

2 Answers

0 votes
answered Oct 13, 2015 by sumit_jaiswalmca
0 votes
answered Oct 13, 2015 by balvant maurya