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

Eager loading issue with inheritance model

    • Type: Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Priority: Icon: Minor - P4 Minor - P4
    • 12_01_17
    • Affects Version/s: 5.1.0
    • Component/s: None
    • Labels:
    • Environment:
      ruby 2.2.2
      rails 4.2.5.2
      mongoid 5.1.0

      Hi, guys
      I have a small issue related with eager loading, I got error during eager loading.
      Here are my models, CourseActivity inherit Activity, so I assume CourseActivity inherit 'belongs_to :group' relationship from Activity.

      class Activity
        belongs_to :group
      end
      
      class CourseActivity < Activity
        has_one :assignment
      end
      
      class Group
        has_many :activities, class_name: 'Activity'
      end
      
      class Assignment
        belongs_to :course_activity
      end
      
      # failed with eager loader
      CourseActivity.includes(:group, :assignment).to_a.count
      # undefined method `assignment' for #<Group:0x007fbec6b2a020>
      
      

      However if I add 'belongs_to :group' relationship in CourseActivity, eager loading works fine

      class CourseActivity < Activity
        belongs_to :group
        has_one :assignment
      end
      
      # works perfect
      CourseActivity.includes(:group, :assignment).to_a.count
      
      

      I also enable preload_models in mongoid

      #mongoid.yml
      preload_models: true
      

      I don't know that is caused by polymorphism belongs_to, since found on website "Eager loaded is supported on all relations with the exception of polymorphic belongs_to associations."

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

              Created:
              Updated:
              Resolved: