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

Eager loading does not work with count

    • Type: Icon: Bug Bug
    • Resolution: Gone away
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 7.1.2
    • Component/s: Attributes
    • Labels:
      None
    • Environment:
      rails 5.2, mongoid 7.1, mongo 4.2

      I have two models with has_many and belongs_to defined.  The following two queries always produce exactly n+1 queries

       

      A.where(:ids.in => ids).includes(:bs).collect{|x| x.bs.count}

      A.where(:ids.in => ids).collect{|x| x.bs.count}

       

      The includes has no effect.  Here are the specific relations:

       

      class Case

        has_many :case_statuses, dependent: :destroy

       ...

       

       

      class CaseStatus

        belongs_to :case, index: true, inverse_of: :case_statuses

       

      And given an array of ids, ids (in this case there are 10 ids), both queries produce exaclty 11 mongo queries:

       

      Case.where(:id.in => ids).includes(:case_statuses).collect{|x| x.case_statuses.count}

      Case.where(:id.in => ids).collect{|x| x.case_statuses.count}

       

      i.e. includes does not reduce the fired mongo queries from 11 to 1 as expected. 

       

      Thanks for any help,
      kevin

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            kevin@mainstreetcomputing.com Kevin Chugh
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: