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

Adding pointers in embedded document corrupts the pointed document

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 7.0.0
    • Affects Version/s: None
    • Component/s: None
    • None

      Hello,

      I'm using mongo (1.3.1), mongoid (4.0.0), moped (2.0.2), rails (4.1.1), ruby 2.1.5p273

      This story involves two objects used to build a tree of Steps, with embedded Links documents.
      Links are stored in an array within their Step, as embedded documents. Each Link contains a pointer to a step_id.

      Here are the minimal objects that can reproduce the behaviour :

      class DemoStep
        include Mongoid::Document
        field :name, type: String
        embeds_many :demo_links
      end
      
      class DemoLink
        include Mongoid::Document
        field :label, type: String
        embedded_in :demo_step
        belongs_to :next, class_name: 'DemoStep'
      end
      

      I create two of these Steps for the demo and confirm that s1 and s2 have no Link for now :

      s1 = DemoStep.create id: 1, name: "Step 1"
      s2 = DemoStep.create id: 2, name: "Step 2"
      
      s1.demo_links
      => []
      s2.demo_links
      => []
      

      Finally, I add a Link inside the freshly created

      Unable to find source-code formatter for language: s. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      1

      , its

      Unable to find source-code formatter for language: next```. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
       attribute fed with 

      s2

      .
      
      The Link is popping exactly as expected, and accessing its 

      next

       relation seems correct too :
      
      

      l = s1.demo_links.create label: "Linking s1 to s2", next: s2
      => #<DemoLink _id: 54920d89626f620958000000, label: "Linking s1 to s2", next_id: 20>

      s1.demo_links
      => [#<DemoLink _id: 54920d89626f620958000000, label: "Linking s1 to s2", next_id: 20>]

      l.next
      => #<DemoStep _id: 20, name: "Step 2">

      
      But surprise! s2 suddenly receives something for its 

      links

       relation:
      
      

      s2.demo_links
      => #<DemoLink _id: 54920d89626f620958000000, label: "Linking s1 to s2", next_id: 20>

      
      Even stranger, the collection is not an array but just a link. The Link I just created and added to 

      s1

       pointing to 

      s2

       now corrupted 

      s2{{`}} itself too.

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            bmedici bmedici
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: