Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-2883

recursively_embeds_many raises Mongoid::Errors::MixedRelations when saving related docs

    XMLWordPrintable

    Details

    • Type: Task
    • Status: Closed
    • Resolution: Done
    • Affects Version/s: None
    • Fix Version/s: 3.1.3
    • Component/s: None
    • Labels:

      Description

      Using 3.1.0-stable

      class Item
        include Mongoid::Document
       
        belongs_to :project
        recursively_embeds_many
      end
       
      class Project
        include Mongoid::Document
        has_many :items
      end
      

      When trying to save a project you get:

      Mongoid::Errors::MixedRelations:
      Problem:
      Referencing a Item document from the Project document via a relational association is not allowed since the Item is embedded.
      Summary:
      In order to properly access a Item from Project the reference would need to go through the root document of Item. In a simple case this would require Mongoid to store an extra foreign key for the root, in more complex cases where Item is multiple levels deep a key would need to be stored for each parent up the hierarchy.
      Resolution:
      Consider not embedding Item, or do the key storage and access in a custom manner in the application code.

        Attachments

          Activity

            People

            Assignee:
            durran Durran Jordan
            Reporter:
            bitzesty Matthew Ford
            Participants:
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: