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

Is it possible to do eager loading (includes) with a different foreign key (not _id)

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

      includes(...) works great in most cases where you are using standard has_many and belongs_to. But we have a special referenced relationship that does not use this field and were wondering if it was possible to emulate something like includes using a different field.

      We have something like:

      #parents = Parent.all.includes(:children)
      parents = Parent.all
      parents.each do |p|
        p.children.each do |c|
          # N+1 query
        end
      end
      

      Seems like there should be some hack here to get the children in there with one query. Thanks!

            Assignee:
            Unassigned Unassigned
            Reporter:
            barmstrong Brian Armstrong
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: