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

Eager loading / Identity map doesn't work i.c.w. inheritance

    • Type: Icon: Task Task
    • Resolution: Done
    • 3.0.0
    • Affects Version/s: None
    • Component/s: None
    • Labels:

      Unless I'm mistaken,

      class A
        include Mongoid::Document
      end
      
      class B < A
        belongs_to :c
      end
      
      class C
        include Mongoid::Document
        has_one :b
      end
      
      C.create
      C.create
      B.create(:c => C.last)
      
      Mongoid.identity_map_enabled = true
      
      C.all.includes(:b).collect(&:b)
      

      should only result in two queries, namely

        QUERY        database=mongoid_test collection=cs selector={} flags=[] limit=0 skip=0 fields=nil (0.0006ms)
        QUERY        database=mongoid_test collection=as selector={"c_id"=>{"$in"=>["4ff487828ef895c465000001", "4ff487878ef895c465000003"]}, "_type"=>{"$in"=>["B"]}} flags=[] limit=0 skip=0 fields=nil (0.0005ms)
      

      While those two are indeed performed, a third one also is:

      QUERY        database=mongoid_test collection=as selector={"c_id"=>"4ff487828ef895c465000001", "_type"=>{"$in"=>["B"]}} flags=[] limit=-1 skip=0 fields=nil (0.0005ms)
      

      So it looks like eager loading works but the identity map doesn't know how to properly handle subclasses and thus "forgets" that eager loading has taken place. Is this by design or might it be a bug?

      I am seeing this behaviour with mongoid master on Ruby 1.9.3p194.

            Assignee:
            Unassigned Unassigned
            Reporter:
            niels Niels Ganser
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: