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

Mongoid creates empty objects after saving embedded document.

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

      For example I have three models: Parent -> Child1 -> Child2

      Unable to find source-code formatter for language: `ruby. 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
      require "spec_helper"
      
      class Parent
        include Mongoid::Document
        embeds_many :childs, class_name: "Child1"
      end
      
      class Child1
        include Mongoid::Document
        embedded_in :parent
        embeds_many :childs, class_name: "Child2"
      end
      
      class Child2
        include Mongoid::Document
        field :age, type: Integer, default: 0
        embedded_in :child, class_name: "Child1"
      end
      
      describe "Saving embedded document" do
        before :each do
          Parent.delete_all
          parent = Parent.create
          parent.childs << Child1.new
          5.times do
            parent.childs.first.childs << Child2.new
          end
        end
      
        it "should not create childs for the 'root' model " do
          child = Parent.first.childs.first.childs.last
          child.age = 10
          child.save
          Parent.first.childs.count.should eql 1
        end
      end
      

      `
      In this test I tried change field of the last embedded document but in the result it also create (Child2.count - 1) empty documents of Child1 instance (in this example 4).

      This bug is very unusual because ids of created objects increment on every load from db.

      P.S. This bug only in last version 3.1.0 (in 3.0.18 all works fine)

            Assignee:
            Unassigned Unassigned
            Reporter:
            Goozler Goozler [X]
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: