aboutsummaryrefslogblamecommitdiffstats
path: root/activerecord/lib/active_record/associations/preloader/through_association.rb
blob: eaceb6c3d9f02311bbf825477fc76024735d5c6e (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14













                                        

















                                                                                 
 


                                                                       


                                                           
 




                                                                        



               
                         
                                                   

                                  
                                                                         
              
                                                       

                                                                                                   

               

                                                                                

             
               




           
module ActiveRecord
  module Associations
    class Preloader
      module ThroughAssociation #:nodoc:

        def through_reflection
          reflection.through_reflection
        end

        def source_reflection
          reflection.source_reflection
        end

        def associated_records_by_owner
          left_loader = Preloader.new(owners,
                                      through_reflection.name,
                                      through_scope)
          left_loader.run

          should_reset = (through_scope != through_reflection.klass.unscoped) ||
             (reflection.options[:source_type] && through_reflection.collection?)

          through_records = owners.map do |owner, h|
            association = owner.association through_reflection.name

            x = [owner, Array(association.reader), association]

            # Dont cache the association - we would only be caching a subset
            association.reset if should_reset

            x
          end

          middle_records = through_records.map { |rec| rec[1] }.flatten

          preloader = Preloader.new(middle_records,
                                    source_reflection.name,
                                    reflection_scope)
          preloader.run

          through_records.each_with_object({}) { |(lhs,middles,assoc),h|
            h[lhs] = middles.flat_map { |r|
              r.send(source_reflection.name)
            }.compact
          }
        end

        private

        def through_scope
          scope = through_reflection.klass.unscoped

          if options[:source_type]
            scope.where! reflection.foreign_type => options[:source_type]
          else
            unless reflection_scope.where_values.empty?
              scope.includes_values = Array(reflection_scope.values[:includes] || options[:source])
              scope.where_values    = reflection_scope.values[:where]
            end

            scope.references! reflection_scope.values[:references]
            scope.order! reflection_scope.values[:order] if scope.eager_loading?
          end

          scope
        end
      end
    end
  end
end