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

Embedded documents not setting parent association if declared with a class_name having "::" prefix

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Fixed
    • Affects Version/s: 7.1.1, 7.0.7
    • Fix Version/s: 7.1.2, 7.0.8
    • Component/s: Associations
    • Labels:
      None
    • Environment:
      System : Linux 5.6.6-arch1-1 #1 SMP PREEMPT Tue, 21 Apr 2020 10:35:16 +0000 x86_64 GNU/Linux
      Ruby : ruby 2.6.6p146 (2020-03-31 revision 67876) [x86_64-linux]
      Mongodb : v3.4.22

      Description

      Hello,

      First, I'm using mongoid 7.0.7, but I've tested this on mongoid 7.1.1 and the results are the same.
      This bug was not present in mongoid 6.2.1 .

      About the bug :
      When we declare an embeds_one/embeds_many field on a document, and we specify a class_name with a "::" root namespace (ie "::Test::Foobar"), the parent of this embedded document will not be set, and will return nil.

      What I found is that the method determine_inverses in Mongoid::Association::Embedded::EmbeddedIn/Mongoid::Association::Embedded::EmbedsMany/Mongoid::Association::Embedded::EmbedsOne have this line : rel.relation_class_name == inverse_class_name

      rel.relation_class_name return the value given by the option class_name, the string with the root namespace "::", but inverse_class_name return the class name without any leading "::", because it's simply a call to `name` on the class of the parent.

      Here's some code showing this behaviour :

      #### Test with embeds_one
      class Test
       include Mongoid::Document
       
      embeds_one :test_foobar, class_name: "::Test::Foobar"
      end
       
      class Test::Foobar
       include Mongoid::Document
       
      embedded_in :test_parent, class_name: "::Test" # Doesn't work with this line
       #embedded_in :test_parent, class_name: "Test" # Work with this line
      end
       
      t=Test.new
      t.test_foobar = Test::Foobar.new
      # Should NOT be nil
      t.test_foobar.test_parent
       
       
      #### Test with embeds_many
      class Test
       include Mongoid::Document
       
      embeds_many :test_foobars, class_name: "::Test::Foobar"
      end
       
      class Test::Foobar
       include Mongoid::Document
       
      embedded_in :test_parent, class_name: "::Test" # Doesn't work with this line
       #embedded_in :test_parent, class_name: "Test" # Work with this line
      end
      t=Test.new
      f=t.test_foobars.new
      # Should NOT be nil
      f.test_parent
      

        Attachments

          Activity

            People

            Assignee:
            oleg.pudeyev Oleg Pudeyev
            Reporter:
            thomaskuntz67@gmail.com Thomas Kuntz
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: