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

Require :as on opposite side of polymorphic associations

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: Associations
    • Labels:
      None

      require 'bundler/inline'
      
      gemfile do
        source 'https://rubygems.org'
      
        gem 'mongoid'
      end
      
      Tempfile.open(%w(config .yml)) do |f|
        uri = 'mongodb://mongo/test'
      
        f.write(<<~YAML)
      development:
        clients:
          default:
            uri: #{uri}
        YAML
        f.flush
      
        Mongoid.load!(f.path, 'development')
        Mongoid::Clients.clients.each do |_, client|
          client.logger.level = Logger::ERROR
        end
      end
      
      class Parent
        include Mongoid::Document
      
        embeds_one :embedded_child
      end
      
      class EmbeddedChild
        include Mongoid::Document
      
        embedded_in :parent, polymorphic: true
      end
      
      EmbeddedChild.create!(parent: Parent.create!)
      
      __END__
      Mongoid::Errors::InvalidPath:
      message:
        Having a root path assigned for EmbeddedChild is invalid.
      summary:
        Mongoid has two different path objects for determining the location of a document in the database, Root and Embedded. This error is raised when an embedded document somehow gets a root path assigned.
      resolution:
        Most likely your embedded model, EmbeddedChild is also referenced via a has_many from a root document in another collection. Double check the association definitions and fix any instances where embedded documents are improperly referenced from other collections.
      from /usr/local/bundle/gems/mongoid-7.1.2/lib/mongoid/atomic/paths/root.rb:36:in `insert_modifier'
      

      Is this intentional?

            Assignee:
            Unassigned Unassigned
            Reporter:
            mtsmfm@quipper.com Fumiaki Matsushima
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: