Module: ActiveRecord::Associations::Preloader::ManualAssociationPreloader

Defined in:
lib/autoload/active_record/associations/preloader/manual_association_preloader.rb

Instance Method Summary collapse

Instance Method Details

#initialize(klass, owners, reflection, records) ⇒ Object


3
4
5
6
7
8
9
10
11
# File 'lib/autoload/active_record/associations/preloader/manual_association_preloader.rb', line 3

def initialize(klass, owners, reflection, records)
  super(klass, owners, reflection, nil)

  @records_by_owner = records.each_with_object({}) do |record, h|
    owner_id = convert_key(record[association_key_name])
    records = (h[owner_id] ||= [])
    records << record
  end
end

#records_for(ids) ⇒ Object


17
18
19
20
21
22
23
24
# File 'lib/autoload/active_record/associations/preloader/manual_association_preloader.rb', line 17

def records_for(ids)
  ids.flat_map { |id| @records_by_owner[id] }.tap(&:compact!).tap do |result|
    # In ActiveRecord 5.0.1, an ActiveRecord::Relation is expected to be returned.
    result.define_singleton_method(:load) do
      self
    end
  end
end

#scopeObject

Raises:

  • (NotImplementedError)

13
14
15
# File 'lib/autoload/active_record/associations/preloader/manual_association_preloader.rb', line 13

def scope
  raise NotImplementedError
end