Mongoid :: Errors :: MixedRelations + save

I have 3 models:

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 

I have to add a built-in relationship between user and user_hobby, but after that (in my tests) I have this error when I want to keep interest:

Mongoid :: Errors :: MixedRelations: referencing a (n) UserHobby document from an Interest document through a relational association is not allowed from the moment the UserHobby is inserted.

I watched this topic, but did not help me, but Mongoid :: Errors :: MixedRelations

Thanks.

+4
source share
1 answer

This is the main problem that your UserHobby model is built into User. According to mongodb, if a document is embedded, it cannot refer to any other model except the one in which it is embedded. If you want to refer to a UserHobby document from another model, then UserHobby and User should not have an inline relationship.

+5
source

All Articles