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

Multiple embedded documents of the same class but different class_name causes the wrong thing to get set

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 5.0.2
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      It'll be easier to show with a failing test case this problem..

      require "spec_helper"
      
      class ChildTestClass
        include Mongoid::Document
          
        embedded_in :parent_test_class, inverse_of: :child_test_one
        embedded_in :parent_test_class, inverse_of: :child_test_two
        
        field :value, type: String
      end
      
      class ParentTestClass
        include Mongoid::Document
        
        embeds_one :child_test_one, cascade_callbacks: true, class_name: "ChildTestClass"
        embeds_one :child_test_two, cascade_callbacks: true, class_name: "ChildTestClass"
      end
      
      describe "a bug with mutliple embedded documents of the same type but different name" do
        it "should set them correctly" do
          parent = ParentTestClass.new
          
          ct1 = ChildTestClass.new(value: "A")
          ct2 = ChildTestClass.new(value: "B")
          ct3 = ChildTestClass.new(value: "C")
          
          parent.child_test_one = ct1
          parent.child_test_two = ct2
          parent.save!
          
          parent.reload.child_test_one.value.should == "A"
          parent.reload.child_test_two.value.should == "B"
          
          parent.child_test_one = ct3
          parent.save!
        
          parent.reload.child_test_two.value.should == "B" # this is now "C"
          parent.reload.child_test_one.value.should == "C" # this is now nil
        end
      end
      

      This is failing on 3.1.0-stable branch

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

              Created:
              Updated:
              Resolved: