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

Namespaced models in relations are not found

    XMLWordPrintable

Details

    • Bug
    • Status: Closed
    • Minor - P4
    • Resolution: Fixed
    • 7.0.0
    • 7.0.2
    • None
    • Ubuntu, Ruby 2.5.1, Mongoid 7.0.0, MongoDB 3.6.3

    Description

      Setting up relations with models that are in a namespace does not work:

      # model_a.rb
      module Space
        class ModelA
          include Mongoid::Document
       
          belongs_to :model_b
        end
      end
       
      # model_b.rb
      module Space
        class ModelB
          include Mongoid::Document
       
          has_one :model_a
        end
      end
       
      # app.rb
      require_relative 'model_a.rb'
      require_relative 'model_b.rb'
       
      a = Space::ModelA.create
      b = Space::ModelB.create(model_a: a)
       
      ####### Result #######
      Traceback (most recent call last):
      	29: from app.rb:16:in `<main>'
      	28: from /usr/local/lib/ruby/gems/2.5.0/gems/mongoid-7.0.0/lib/mongoid/persistable/creatable.rb:146:in `create'
      	27: from /usr/local/lib/ruby/gems/2.5.0/gems/mongoid-7.0.0/lib/mongoid/threaded/lifecycle.rb:161:in `_creating'
      	26: from /usr/local/lib/ruby/gems/2.5.0/gems/mongoid-7.0.0/lib/mongoid/persistable/creatable.rb:150:in `block in create'
      	25: from /usr/local/lib/ruby/gems/2.5.0/gems/mongoid-7.0.0/lib/mongoid/persistable/creatable.rb:150:in `new'
      	24: from /usr/local/lib/ruby/gems/2.5.0/gems/mongoid-7.0.0/lib/mongoid/document.rb:114:in `initialize'
      	23: from /usr/local/lib/ruby/gems/2.5.0/gems/mongoid-7.0.0/lib/mongoid/threaded/lifecycle.rb:89:in `_building'
      	22: from /usr/local/lib/ruby/gems/2.5.0/gems/mongoid-7.0.0/lib/mongoid/document.rb:119:in `block in initialize'
      	21: from /usr/local/lib/ruby/gems/2.5.0/gems/mongoid-7.0.0/lib/mongoid/attributes/processing.rb:29:in `process_attributes'
      	20: from /usr/local/lib/ruby/gems/2.5.0/gems/mongoid-7.0.0/lib/mongoid/attributes/processing.rb:123:in `process_pending'
      	19: from /usr/local/lib/ruby/gems/2.5.0/gems/mongoid-7.0.0/lib/mongoid/attributes/processing.rb:135:in `process_relations'
      	18: from /usr/local/lib/ruby/gems/2.5.0/gems/mongoid-7.0.0/lib/mongoid/attributes/processing.rb:135:in `each_pair'
      	17: from /usr/local/lib/ruby/gems/2.5.0/gems/mongoid-7.0.0/lib/mongoid/attributes/processing.rb:140:in `block in process_relations'
      	16: from /usr/local/lib/ruby/gems/2.5.0/gems/mongoid-7.0.0/lib/mongoid/association/accessors.rb:247:in `block (2 levels) in define_setter!'
      	15: from /usr/local/lib/ruby/gems/2.5.0/gems/mongoid-7.0.0/lib/mongoid/association/accessors.rb:140:in `without_autobuild'
      	14: from /usr/local/lib/ruby/gems/2.5.0/gems/mongoid-7.0.0/lib/mongoid/association/accessors.rb:256:in `block (3 levels) in define_setter!'
      	13: from /usr/local/lib/ruby/gems/2.5.0/gems/mongoid-7.0.0/lib/mongoid/association/accessors.rb:25:in `__build__'
      	12: from /usr/local/lib/ruby/gems/2.5.0/gems/mongoid-7.0.0/lib/mongoid/association/accessors.rb:42:in `create_relation'
      	11: from /usr/local/lib/ruby/gems/2.5.0/gems/mongoid-7.0.0/lib/mongoid/association/referenced/has_one/buildable.rb:23:in `build'
      	10: from /usr/local/lib/ruby/gems/2.5.0/gems/mongoid-7.0.0/lib/mongoid/association/referenced/has_one/buildable.rb:31:in `clear_associated'
      	 9: from /usr/local/lib/ruby/gems/2.5.0/gems/mongoid-7.0.0/lib/mongoid/association/relatable.rb:304:in `inverse'
      	 8: from /usr/local/lib/ruby/gems/2.5.0/gems/mongoid-7.0.0/lib/mongoid/association/relatable.rb:121:in `inverses'
      	 7: from /usr/local/lib/ruby/gems/2.5.0/gems/mongoid-7.0.0/lib/mongoid/association/referenced/has_one.rb:187:in `determine_inverses'
      	 6: from /usr/local/lib/ruby/gems/2.5.0/gems/mongoid-7.0.0/lib/mongoid/association/relatable.rb:160:in `klass'
      	 5: from /usr/local/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/core_ext/string/inflections.rb:66:in `constantize'
      	 4: from /usr/local/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/inflector/methods.rb:267:in `constantize'
      	 3: from /usr/local/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/inflector/methods.rb:267:in `inject'
      	 2: from /usr/local/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/inflector/methods.rb:267:in `each'
      	 1: from /usr/local/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/inflector/methods.rb:269:in `block in constantize'
      /usr/local/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/inflector/methods.rb:269:in `const_get': uninitialized constant ModelA (NameError)
      Did you mean?  Module
      

      This worked perfectly in version 6.4

      It can be solved by adding `class_name: 'Space::ModelA' etc to the relations.

      Is relations with namespaced models broken in version 7, or is this change's documenation missing?

      Attachments

        Issue Links

          Activity

            People

              sam.rossi@mongodb.com Samuel Rossi (Inactive)
              dsen David Sennerlöv
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: