/usr/lib/ruby/vendor_ruby/active_record/associations/preloader/has_and_belongs_to_many.rb is in ruby-activerecord-3.2 3.2.16-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | 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
|