Sha256: 282cfb3c34e31fbd32354a062e30935fd0bbba7d48185e56890f8ea5fbe5ec6a

Contents?: true

Size: 1.7 KB

Versions: 29

Compression:

Stored size: 1.7 KB

Contents

module ActiveRecord
  module Associations
    class Preloader
      class HasAndBelongsToMany < CollectionAssociation #:nodoc:
        attr_reader :join_table

        def initialize(klass, records, reflection, preload_options)
          super
          @join_table = Arel::Table.new(reflection.join_table).alias('t0')
        end

        # Unlike the other associations, we want to get a raw array of rows so that we can
        # access the aliased column on the join table
        def records_for(ids)
          scope = super
          klass.connection.select_all(scope.arel, 'SQL', scope.bind_values)
        end

        def owner_key_name
          reflection.active_record_primary_key
        end

        def association_key_name
          'ar_association_key_name'
        end

        def association_key
          join_table[reflection.foreign_key]
        end

        private

        # Once we have used the join table column (in super), we manually instantiate the
        # actual records, ensuring that we don't create more than one instances of the same
        # record
        def associated_records_by_owner
          records = {}
          super.each_value do |rows|
            rows.map! { |row| records[row[klass.primary_key]] ||= klass.instantiate(row) }
          end
        end

        def build_scope
          super.joins(join).select(join_select)
        end

        def join_select
          association_key.as(Arel.sql(association_key_name))
        end

        def join
          condition = table[reflection.association_primary_key].eq(
            join_table[reflection.association_foreign_key])

          table.create_join(join_table, table.create_on(condition))
        end
      end
    end
  end
end

Version data entries

29 entries across 29 versions & 2 rubygems

Version Path
activerecord-4.0.13 lib/active_record/associations/preloader/has_and_belongs_to_many.rb
activerecord-4.0.13.rc1 lib/active_record/associations/preloader/has_and_belongs_to_many.rb
activerecord-4.0.11.1 lib/active_record/associations/preloader/has_and_belongs_to_many.rb
activerecord-4.0.12 lib/active_record/associations/preloader/has_and_belongs_to_many.rb
activerecord-4.0.11 lib/active_record/associations/preloader/has_and_belongs_to_many.rb
activerecord-4.0.10 lib/active_record/associations/preloader/has_and_belongs_to_many.rb
activerecord-4.0.10.rc2 lib/active_record/associations/preloader/has_and_belongs_to_many.rb
activerecord-4.0.10.rc1 lib/active_record/associations/preloader/has_and_belongs_to_many.rb
activerecord-4.0.9 lib/active_record/associations/preloader/has_and_belongs_to_many.rb
activerecord-4.0.8 lib/active_record/associations/preloader/has_and_belongs_to_many.rb
activerecord-4.0.7 lib/active_record/associations/preloader/has_and_belongs_to_many.rb
activerecord-4.0.6 lib/active_record/associations/preloader/has_and_belongs_to_many.rb
activerecord-4.0.6.rc3 lib/active_record/associations/preloader/has_and_belongs_to_many.rb
activerecord-4.0.6.rc2 lib/active_record/associations/preloader/has_and_belongs_to_many.rb
activerecord-4.0.6.rc1 lib/active_record/associations/preloader/has_and_belongs_to_many.rb
activerecord-4.0.5 lib/active_record/associations/preloader/has_and_belongs_to_many.rb
activerecord-4.0.4 lib/active_record/associations/preloader/has_and_belongs_to_many.rb
activerecord-4.0.4.rc1 lib/active_record/associations/preloader/has_and_belongs_to_many.rb
activerecord-4.0.3 lib/active_record/associations/preloader/has_and_belongs_to_many.rb
activerecord-4.0.2 lib/active_record/associations/preloader/has_and_belongs_to_many.rb