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

Mongoid::Errors::NoMetadata when parent has 2 different embeds_one relations to the same child

    • Type: Icon: Task Task
    • Resolution: Done
    • 3.0.2
    • Affects Version/s: None
    • Component/s: None

      I think a code snippet is the easiest way to explain the issue I'm having:

      class Parent
        include Mongoid::Document
      
        embeds_one :first_child, class_name: "Child", as: :childable
        embeds_one :second_child, class_name: "Child", as: :childable
      end
      
      class Child
        include Mongoid::Document
      
        embedded_in :parent, inverse_of: :childable
      end
      
      [2] pry(main)> p = Parent.create
      => #<Parent _id: 50077051312f91a556000001, _type: nil>
      [3] pry(main)> p.first_child = Child.new
      => #<Child _id: 50077056312f91a556000002, _type: nil>
      [4] pry(main)> p.first_child = Child.new
      => #<Child _id: 50077057312f91a556000003, _type: nil>
      [5] pry(main)> p.first_child = Child.new
      Mongoid::Errors::NoMetadata: 
      Problem:
        Metadata not found for document of type Child.
      Summary:
        Mongoid sets the metadata of a relation on the document when it is either loaded from within the relation, or added to one. The presence of the metadata is required in order to provide various functionality around relations. Most likely you are getting this error because the document is embedded and was attempted to be persisted without being associated with a parent, or the relation was not properly defined.
      Resolution:
        Ensure that your relations on the Child model are all properly defined, and that the inverse relations are also properly defined. Embedded relations must have both the parent (embeds_one/embeds_many) and the inverse (embedded_in) present in order to work properly.
      

      When replacing first_child with a Child.new for the third time I get the error.

            Assignee:
            durran Durran Jordan
            Reporter:
            gcirne Guilherme Cirne
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: