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

Eager_load won't trigger with includes() for inherited classes

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

      Consering :

      class Thing
      include Mongoid::Document

      has_many :tasks
      end

      class Kase < Thing
      end
      class Deal < Thing
      end

      class Task
      include Mongoid::Document

      belongs_to :thing
      end

      My Thing class is never instantiated. Only Deal & Kase.

      Doing :

      @tasks = Task.includes(:thing)
      @tasks.each do |task|
      puts task.thing.name #for example
      end

      won't trigger the eager load :

      MONGODB max_simple_crm_development['tasks'].find(

      {"tenant_id"=>BSON::ObjectId('4e7618e6f581f90c73000001'), :done_at=>nil}

      ,

      {"thing_id"=>1}

      )
      MONGODB max_simple_crm_development['things'].find({"_id"=>{"$in"=>[BSON::ObjectId('4e6a6ca8f581f95883000003'), BSON::ObjectId('4e9bdd56f581f90a5d000004'), BSON::ObjectId('4dc80859f581f90944000014')]}})

      MONGODB max_simple_crm_development['tasks'].find(

      {"tenant_id"=>BSON::ObjectId('4e7618e6f581f90c73000001'), :done_at=>nil}

      ).limit(50).sort([[:due_at, :asc]])

      MONGODB max_simple_crm_development['things'].find({:_id=>BSON::ObjectId('4e6a6ca8f581f95883000003')}).limit(-1).sort([[:_id, :asc]])

      The document with id=4e6a6ca8f581f95883000003 is loaded via the includes special criteria (1st & 2nd line of the log).
      But, it is still fetched from DB as the last line of the log shows.

      And as its class is Kase, it is stored in the Kase "identity_map repository" (documents_for(:kase)).
      https://github.com/mongoid/mongoid/blob/master/lib/mongoid/relations/referenced/in.rb#L129

      But on the criteria emerging from the relation to fetch the Kase (that is referenced as a Thing in the metadata),
      the "from_map_or_db" fucntion looks in the "Thing identity_map repository" (documents_for(:thing)).

      At less, that is what i understood.

            Assignee:
            Unassigned Unassigned
            Reporter:
            maximeg Maxime Garcia
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: