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

Mongoid creates empty objects after saving embedded document.

    XMLWordPrintable

Details

    • Task
    • Status: Closed
    • Resolution: Done
    • None
    • 3.1.0
    • None

    Description

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

      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)

      Attachments

        Activity

          People

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

            Dates

              Created:
              Updated:
              Resolved: