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

embedded_in with polymorphic and touch results in load_missing_constant

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 7.0.2
    • Affects Version/s: 7.0.0, 7.0.1
    • Component/s: None
    • None
    • Environment:
      Ruby 2.3.1, Rails 5.2.0
    • 1

       

      Defining an embedded_in relationship with both polymorphic and touch results in load_missing_constant

      The following setup

      class User
       include Mongoid::Document
       embeds_many :locks, class_name: 'Lock'
      end
      class Role
       include Mongoid::Document
       embeds_many :locks, class_name: 'Lock'
      end
      class Lock
       include Mongoid::Document
       embedded_in :owner, polymorphic: true, touch: true
      end
      

      Throws

      .../gems/ruby-2.3.1/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:503:in `load_missing_constant': Unable to autoload constant Owner, expected /Users/abj/Dropbox/Work/Apparatus/devel/apparatus/app/models/concerns/apparatus/abilities/owner.rb to define it (LoadError).../gems/ruby-2.3.1/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:503:in `load_missing_constant': Unable to autoload constant Owner, expected /Users/abj/Dropbox/Work/Apparatus/devel/apparatus/app/models/concerns/apparatus/abilities/owner.rb to define it (LoadError) from .../gems/ruby-2.3.1/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:193:in `const_missing' from .../gems/ruby-2.3.1/gems/activesupport-5.2.0/lib/active_support/inflector/methods.rb:283:in `const_get' from .../gems/ruby-2.3.1/gems/activesupport-5.2.0/lib/active_support/inflector/methods.rb:283:in `block in constantize' from .../gems/ruby-2.3.1/gems/activesupport-5.2.0/lib/active_support/inflector/methods.rb:281:in `each' from .../gems/ruby-2.3.1/gems/activesupport-5.2.0/lib/active_support/inflector/methods.rb:281:in `inject' from .../gems/ruby-2.3.1/gems/activesupport-5.2.0/lib/active_support/inflector/methods.rb:281:in `constantize' from .../gems/ruby-2.3.1/gems/activesupport-5.2.0/lib/active_support/core_ext/string/inflections.rb:68:in `constantize' from .../gems/ruby-2.3.1/gems/mongoid-7.0.1/lib/mongoid/association/relatable.rb:160:in `klass' from .../gems/ruby-2.3.1/gems/mongoid-7.0.1/lib/mongoid/association/touchable.rb:83:in `define_relation_touch_method' from .../gems/ruby-2.3.1/gems/mongoid-7.0.1/lib/mongoid/association/touchable.rb:57:in `define_touchable!' from .../gems/ruby-2.3.1/gems/mongoid-7.0.1/lib/mongoid/association/relatable.rb:330:in `define_touchable!' from .../gems/ruby-2.3.1/gems/mongoid-7.0.1/lib/mongoid/association/embedded/embedded_in.rb:122:in `setup_instance_methods!' from .../gems/ruby-2.3.1/gems/mongoid-7.0.1/lib/mongoid/association/embedded/embedded_in.rb:43:in `setup!' from .../gems/ruby-2.3.1/gems/mongoid-7.0.1/lib/mongoid/association/macros.rb:197:in `block in define_association!' from .../gems/ruby-2.3.1/gems/mongoid-7.0.1/lib/mongoid/association/macros.rb:196:in `tap' from .../gems/ruby-2.3.1/gems/mongoid-7.0.1/lib/mongoid/association/macros.rb:196:in `define_association!' from .../gems/ruby-2.3.1/gems/mongoid-7.0.1/lib/mongoid/association/macros.rb:54:in `embedded_in' from .../test/models/polymorphic_touch_test.rb:15:in `<class:Lock>'

      This seems to be a result from the recent relations refactor (line 58 in touchable.rb assumes that there's an inverse_class defined on the association). But I'm not sure if it's because it's not supposed to work, or if it's a bug?

      If it's not supposed to work I think it would be good to raise a relevant exception instead. I'm quite sure it used to work in previous versions of mongoid though.

       

            Assignee:
            sam.rossi@mongodb.com Samuel Rossi (Inactive)
            Reporter:
            asgerb Asger Behncke Jacobsen
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: