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

Eager_loads two different relations, which classes inherited from another one class.

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

      Maybe it's a bug or a feature. When eager_loads by 'includes' two different relations, which classes inherited from another one class, expect two different objects.

      Example:

      class A
        include Mongoid::Document
      end
      
      class B < A
        belongs_to :d
      end
      
      class C < A
        belongs_to :d
      end
      
      class D
        include Mongoid::Document
      
        has_one :b
        has_one :c
      end
      
      d = D.create
      B.create(d: d)
      C.create(d: d)
      
      d = D.includes(:b, :c).first
      d.b == d.c # true
      d.b
      # #<C:0x007fd42aa3d418> {
      #      :_id => "50e5bbf798663904ca000004",
      #    :_type => "C",
      #     :d_id => "50e5bbf798663904ca000002"
      # }
      d.c
      # #<C:0x007fd42aa3d418> {
      #      :_id => "50e5bbf798663904ca000004",
      #    :_type => "C",
      #     :d_id => "50e5bbf798663904ca000002"
      # }
      

      Same behavior with 'has_many' relations.
      Check on Mongoid 3.0.17 and master branch.
      I would appreciate additions and corrections.

            Assignee:
            Unassigned Unassigned
            Reporter:
            kslazarev kslazarev
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: