title: Mongoid: relations between embedded documents
body: <p>I have two collections in my DB-</p>
  
  <pre><code>&gt; db.users.find().pretty()
          {
              "_id" : ObjectId("5281c15a617274166f150000"),
              "email" : "aerf@gmail.com",
              "nickname" : "Artem",
              "user_activities" : [
                  {
                      "_id" : ObjectId("5281c19161727415d5000000"),
                      "activity_id" : ObjectId("5281c15a617274166f010000"),
                      ...
                  },
                  {
                      "_id" : ObjectId("5281c19161727415d6000000"),
                      "activity_id" : ObjectId("5281c15a617274166f020000"),
                      ...
                  },
                         ...
              ]
          },
         ....
  </code></pre>
  
  <p>and </p>
  
  <pre><code>&gt; db.monitor_symptoms.find().pretty()
      {
          "_id" : ObjectId("5281c15a617274166f000000"),
          "activities" : [
              {
                  "_id" : ObjectId("5281c15a617274166f010000"),
                  "name" : "ASW"
                          ...
                  },
                  {
                  "_id" : ObjectId("5281c15a617274166f010000"),
                  "name" : "BSW"
                          ...
                  },
                  ...
         ],
         ...
      },
      ...
  </code></pre>
  
  <p>This is supported by the following models in my app-</p>
  
  <pre><code>class User
    include Mongoid::Document
    field :email, type: String, :default =&gt; ""
    field :nickname, type: String, :default =&gt; ""
    embeds_many :user_activities
  end
  
  class UserActivity
    include Mongoid::Document
    embedded_in :user
    has_to_many :activities, inverse_of: nil
  end
  
  class MonitorSymptom
    include Mongoid::Document
    embeds_many :activities
  end
  
  class Activity
    include Mongoid::Document
    field :name, type: String
    embedded_in :monitor_symptom
  end
  </code></pre>
  
  <p>I try next-</p>
  
  <pre><code>2.0.0p0 :090 &gt;   user = User.first
   =&gt; #&lt;User _id: 5281c15a617274166f150000, email: "aerf@gmail.com", nickname: "Artem"
  
  2.0.0p0 :091 &gt; user.user_activities.new(activity_id: "5281c15a617274166f010000")
   =&gt; #&lt;UserActivity _id: 5281c15a617274166f150000, activity_id: "5281c15a617274166f010000"&gt; 
  
  2.0.0p0 :092 &gt; user.save
  </code></pre>
  
  <p>And I catch an error:</p>
  
  <blockquote>
    <p>Mongoid::Errors::MixedRelations:  Problem:   Referencing a(n) Activity
    document from the UserActivity document via a relational association
    is not allowed since the Activity is embedded. Summary:   In order to
    properly access a(n) Activity from UserActivity the reference would
    need to go through the root document of Activity. In a simple case
    this would require Mongoid to store an extra foreign key for the root,
    in more complex cases where Activity is multiple levels deep a key
    would need to be stored for each parent up the hierarchy. Resolution: 
    Consider not embedding Activity, or do the key storage and access in a
    custom manner in the application code.</p>
  </blockquote>
  
  <p>Please help me to make this reference.</p>