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

When using a projection with embeds many relations, accessing the inverse association will now raise MissingAttributeError

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 8.1.6, 9.0.2, 8.0.9
    • Affects Version/s: 8.1.5, 8.0.8, 9.0.1
    • Component/s: Associations
    • None
    • Ruby Drivers
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      After upgrading from v7.5.4 to v8.0.8, I noticed that accessing the parent after using query projection will no longer work. This is also failing in the latest version.

      #!/usr/bin/env ruby
      
      require 'bundler/inline'  
      
      gemfile do
        source 'https://rubygems.org'
        gem 'mongoid', '9.0.0'
      end
      
      require 'mongoid'
      
      Mongoid.configure { |c| c.clients.default = { hosts: ['localhost'], database: 'test' } }
      puts Mongoid::VERSION
      puts Mongo::VERSION
      
      # From https://github.com/mongodb/mongoid/blob/master/spec/mongoid/association/embedded/embeds_many_models.rb
      class EmmCongress
        include Mongoid::Document
        include Mongoid::Timestamps
      
        embeds_many :legislators, class_name: 'EmmLegislator'
      end
      
      class EmmLegislator
        include Mongoid::Document
      
        embedded_in :congress, class_name: 'EmmCongress'
      
        field :a, type: Integer, default: 0
        field :b, type: Integer, default: 0
      end   
      
      # From https://github.com/mongodb/mongoid/blob/master/spec/mongoid/association/embedded/embeds_many_query_spec.rb
      
      # setup
      congress = EmmCongress.new
      congress.legislators << EmmLegislator.new(a: 1, b: 2)
      congress.save!
      
      # let
      congress = EmmCongress.only(:id, "legislators.a").first
      p congress
      # returns #<EmmCongress _id: 669dca95b82c2d0201f76ce3, created_at: nil, updated_at: nil>
      
      legislator = congress.legislators.first
      p legislator.congress
      # expect to get same value as above but instead got ActiveModel::MissingAttributeError (Missing attribute: 'congress')
      # this works in 7.5.4

       
       

            Assignee:
            dmitry.rybakov@mongodb.com Dmitry Rybakov
            Reporter:
            kokchun.lim@braze.com Joe Lim
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: