module Torque::PostgreSQL::Associations::Preloader::Association

Public Instance Methods

run() click to toggle source

For reflections connected through an array, make sure to properly decuple the list of ids and set them as associated with the owner

Calls superclass method
# File lib/torque/postgresql/associations/preloader/association.rb, line 13
def run
  return super unless connected_through_array?
  send("run_array_for_#{@reflection.macro}")
end

Private Instance Methods

associate_records_to_owner(owner, records) click to toggle source
Calls superclass method
# File lib/torque/postgresql/associations/preloader/association.rb, line 79
def associate_records_to_owner(owner, records)
  return super unless connected_through_array?
  association = owner.association(reflection.name)
  association.loaded!
  association.target.concat(records)
end
groupped_records() click to toggle source
# File lib/torque/postgresql/associations/preloader/association.rb, line 86
def groupped_records
  preloaded_records.group_by do |record|
    convert_key(record[association_key_name])
  end
end
load_records() click to toggle source

This is how Rails 6.1 now load the records

Calls superclass method
# File lib/torque/postgresql/associations/preloader/association.rb, line 46
def load_records
  return super unless connected_through_array?

  @records_by_owner = {}.compare_by_identity
  raw_records = owner_keys.empty? ? [] : records_for(owner_keys)

  @preloaded_records = raw_records.select do |record|
    assignments = false

    ids = convert_key(record[association_key_name])
    owners_by_key.values_at(*ids).flat_map do |owner|
      entries = (@records_by_owner[owner] ||= [])

      if reflection.collection? || entries.empty?
        entries << record
        assignments = true
      end
    end

    assignments
  end
end
records_for(ids, &block) click to toggle source

Build correctly the constraint condition in order to get the associated ids

Calls superclass method
# File lib/torque/postgresql/associations/preloader/association.rb, line 72
def records_for(ids, &block)
  return super unless connected_through_array?
  condition = scope.arel_table[association_key_name]
  condition = reflection.build_id_constraint(condition, ids.flatten.uniq)
  scope.where(condition).load(&block)
end
run_array_for_belongs_to_many() click to toggle source

Specific run for belongs_many association

# File lib/torque/postgresql/associations/preloader/association.rb, line 21
def run_array_for_belongs_to_many
  # Add reverse to has_many
  records = groupped_records
  owners.each do |owner|
    items = records.values_at(*Array.wrap(owner[owner_key_name]))
    associate_records_to_owner(owner, items.flatten)
  end
end
run_array_for_has_many() click to toggle source

Specific run for has_many association

# File lib/torque/postgresql/associations/preloader/association.rb, line 31
def run_array_for_has_many
  # Add reverse to belongs_to_many
  records = Hash.new { |h, k| h[k] = [] }
  groupped_records.each do |ids, record|
    ids.each { |id| records[id].concat(Array.wrap(record)) }
  end

  records.default_proc = nil
  owners.each do |owner|
    associate_records_to_owner(owner, records[owner[owner_key_name]] || [])
  end
end