Sha256: 9d69bb3f3f0b9b1e9eb40cdaca392ac0ef95f19ae832b7883282808b396ffc9a

Contents?: true

Size: 966 Bytes

Versions: 24

Compression:

Stored size: 966 Bytes

Contents

# frozen_string_literal: true

module Mongoid
  module Association
    module Referenced
      class HasMany

        # Eager class for has_many associations.
        class Eager < Association::Referenced::Eager::Base

          private

          def preload
            @docs.each do |d|
              set_relation(d, [])
            end

            entries = Hash.new { |hash, key| hash[key] = [] }
            each_loaded_document do |doc|
              fk = doc.send(key)
              entries[fk] << doc
            end

            entries.each do |id, docs|
              set_on_parent(id, docs)
            end
          end

          def set_relation(doc, element)
            doc.__build__(@association.name, element, @association) unless doc.blank?
          end

          def group_by_key
            @association.primary_key
          end

          def key
            @association.foreign_key
          end
        end
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
mongoid-8.1.7 lib/mongoid/association/referenced/has_many/eager.rb
mongoid-8.1.6 lib/mongoid/association/referenced/has_many/eager.rb
mongoid-8.0.8 lib/mongoid/association/referenced/has_many/eager.rb
mongoid-8.1.5 lib/mongoid/association/referenced/has_many/eager.rb
mongoid-8.1.4 lib/mongoid/association/referenced/has_many/eager.rb
mongoid-8.0.7 lib/mongoid/association/referenced/has_many/eager.rb
mongoid-8.1.3 lib/mongoid/association/referenced/has_many/eager.rb
mongoid-8.1.2 lib/mongoid/association/referenced/has_many/eager.rb
mongoid-8.0.6 lib/mongoid/association/referenced/has_many/eager.rb
mongoid-7.5.4 lib/mongoid/association/referenced/has_many/eager.rb
mongoid-8.1.1 lib/mongoid/association/referenced/has_many/eager.rb
mongoid-8.0.5 lib/mongoid/association/referenced/has_many/eager.rb
mongoid-8.1.0 lib/mongoid/association/referenced/has_many/eager.rb
mongoid-7.5.3 lib/mongoid/association/referenced/has_many/eager.rb
mongoid-8.0.4 lib/mongoid/association/referenced/has_many/eager.rb
mongoid-8.0.3 lib/mongoid/association/referenced/has_many/eager.rb
mongoid-7.5.2 lib/mongoid/association/referenced/has_many/eager.rb
mongoid-8.0.2 lib/mongoid/association/referenced/has_many/eager.rb
mongoid-7.5.1 lib/mongoid/association/referenced/has_many/eager.rb
mongoid-7.4.3 lib/mongoid/association/referenced/has_many/eager.rb