Sha256: d602e1d5d006c93fbbbd83c2351cb5987b5027b4230ab4ce8384964e13b04a73

Contents?: true

Size: 1.71 KB

Versions: 187

Compression:

Stored size: 1.71 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(options[: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 do |owner_key, 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

187 entries across 156 versions & 15 rubygems

Version Path
mdg-1.0.1 vendor/bundle/ruby/2.3.0/gems/activerecord-3.2.22.5/lib/active_record/associations/preloader/has_and_belongs_to_many.rb
activerecord-3.2.22.5 lib/active_record/associations/preloader/has_and_belongs_to_many.rb
activerecord-3.2.22.4 lib/active_record/associations/preloader/has_and_belongs_to_many.rb
activerecord-3.2.22.3 lib/active_record/associations/preloader/has_and_belongs_to_many.rb
activerecord-3.2.22.2 lib/active_record/associations/preloader/has_and_belongs_to_many.rb
activerecord-3.2.22.1 lib/active_record/associations/preloader/has_and_belongs_to_many.rb
active_mailer-0.0.10 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/associations/preloader/has_and_belongs_to_many.rb
activerecord-3.2.22 lib/active_record/associations/preloader/has_and_belongs_to_many.rb
judge-2.0.5 vendor/bundle/ruby/2.1.0/gems/activerecord-3.2.12/lib/active_record/associations/preloader/has_and_belongs_to_many.rb
activerecord-3.2.21 lib/active_record/associations/preloader/has_and_belongs_to_many.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.18/lib/active_record/associations/preloader/has_and_belongs_to_many.rb
apl-library-0.0.90 vendor/bundle/ruby/1.8/gems/activerecord-3.2.18/lib/active_record/associations/preloader/has_and_belongs_to_many.rb
apl-library-0.0.90 vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.18/lib/active_record/associations/preloader/has_and_belongs_to_many.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/activerecord-3.2.18/lib/active_record/associations/preloader/has_and_belongs_to_many.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.8/gems/activerecord-3.2.18/lib/active_record/associations/preloader/has_and_belongs_to_many.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/2.1.0/gems/activerecord-3.2.18/lib/active_record/associations/preloader/has_and_belongs_to_many.rb
activerecord-3.2.20 lib/active_record/associations/preloader/has_and_belongs_to_many.rb
activerecord-3.2.19 lib/active_record/associations/preloader/has_and_belongs_to_many.rb
activerecord-3.2.18 lib/active_record/associations/preloader/has_and_belongs_to_many.rb
activerecord-3.2.17 lib/active_record/associations/preloader/has_and_belongs_to_many.rb