eurek
eurek

Reputation: 93

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 must add the embedded relation between user and user_hobby, but after that (in my tests) i have this error when i want to save an interest:

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

I watched this topic but didn't help me as well Mongoid::Errors::MixedRelations

Thanks.

Upvotes: 5

Views: 747

Answers (1)

abhas
abhas

Reputation: 5213

In this the main issue is that your UserHobby model is embedded in User. According to mongodb if a document is embedded it cannot be referenced from any other model except the one in which it is embedded in. If you want to reference the UserHobby document from other model, then UserHobby and User should not have embedded relationship.

Upvotes: 6

Related Questions