123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::Associations::Preloader::Association::LoaderRecords

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: activerecord/lib/active_record/associations/preloader/association.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(loaders, loader_query) ⇒ LoaderRecords

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 61

def initialize(loaders, loader_query)
  @loader_query = loader_query
  @loaders = loaders
  @keys_to_load = Set.new
  @already_loaded_records_by_key = {}

  populate_keys_to_load_and_already_loaded_records
end

Instance Attribute Details

#already_loaded_records_by_key (readonly, private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 75

attr_reader :loader_query, :loaders, :keys_to_load, :already_loaded_records_by_key

#keys_to_load (readonly, private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 75

attr_reader :loader_query, :loaders, :keys_to_load, :already_loaded_records_by_key

#loader_query (readonly, private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 75

attr_reader :loader_query, :loaders, :keys_to_load, :already_loaded_records_by_key

#loaders (readonly, private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 75

attr_reader :loader_query, :loaders, :keys_to_load, :already_loaded_records_by_key

Instance Method Details

#already_loaded_records (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 97

def already_loaded_records
  already_loaded_records_by_key.values.flatten
end

#load_records (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 91

def load_records
  loader_query.load_records_for_keys(keys_to_load) do |record|
    loaders.each { |l| l.set_inverse(record) }
  end
end

#populate_keys_to_load_and_already_loaded_records (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 77

def populate_keys_to_load_and_already_loaded_records
  loaders.each do |loader|
    loader.owners_by_key.each do |key, owners|
      if loaded_owner = owners.find { |owner| loader.loaded?(owner) }
        already_loaded_records_by_key[key] = loader.target_for(loaded_owner)
      else
        keys_to_load << key
      end
    end
  end

  @keys_to_load.subtract(already_loaded_records_by_key.keys)
end

#records

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 70

def records
  load_records + already_loaded_records
end