Class: ActiveRecord::Associations::Preloader::HasAndBelongsToMany

Inherits:
CollectionAssociation show all
Defined in:
activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Attributes inherited from Association

#klass, #model, #owners, #preload_options, #reflection

Instance Method Summary collapse

Methods inherited from Association

#options, #owners_by_key, #preload, #run, #scoped, #table

Constructor Details

#initialize(klass, records, reflection, preload_options) ⇒ HasAndBelongsToMany

Returns a new instance of HasAndBelongsToMany.



7
8
9
10
# File 'activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb', line 7

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

Instance Attribute Details

#join_tableObject (readonly)

Returns the value of attribute join_table



5
6
7
# File 'activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb', line 5

def join_table
  @join_table
end

Instance Method Details

#association_keyObject



27
28
29
# File 'activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb', line 27

def association_key
  join_table[reflection.foreign_key]
end

#association_key_nameObject



23
24
25
# File 'activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb', line 23

def association_key_name
  'ar_association_key_name'
end

#owner_key_nameObject



19
20
21
# File 'activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb', line 19

def owner_key_name
  reflection.active_record_primary_key
end

#records_for(ids) ⇒ Object

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



14
15
16
17
# File 'activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb', line 14

def records_for(ids)
  scope = super
  klass.connection.select_all(scope.arel, 'SQL', scope.bind_values)
end