method
records_by_owner
v6.1.7.7 -
Show latest stable
- Class:
ActiveRecord::Associations::Preloader::ThroughAssociation
records_by_owner()public
No documentation available.
# File activerecord/lib/active_record/associations/preloader/through_association.rb, line 18
def records_by_owner
return @records_by_owner if defined?(@records_by_owner)
source_records_by_owner = source_preloaders.map(&:records_by_owner).reduce(:merge)
through_records_by_owner = through_preloaders.map(&:records_by_owner).reduce(:merge)
@records_by_owner = owners.each_with_object({}) do |owner, result|
through_records = through_records_by_owner[owner] || []
if @already_loaded
if source_type = reflection.options[:source_type]
through_records = through_records.select do |record|
record[reflection.foreign_type] == source_type
end
end
end
records = through_records.flat_map do |record|
source_records_by_owner[record]
end
records.compact!
records.sort_by! { |rhs| preload_index[rhs] } if scope.order_values.any?
records.uniq! if scope.distinct_value
result[owner] = records
end
end