Permalink
Browse files

remove the HABTM preloader

  • Loading branch information...
1 parent 88c0093 commit a03ea3ff97b43340d0904525083bf8bc7a1c6ebc @tenderlove tenderlove committed Oct 2, 2013
View
3 activerecord/lib/active_record/associations/preloader.rb
@@ -42,7 +42,6 @@ class Preloader #:nodoc:
autoload :HasManyThrough, 'active_record/associations/preloader/has_many_through'
autoload :HasOne, 'active_record/associations/preloader/has_one'
autoload :HasOneThrough, 'active_record/associations/preloader/has_one_through'
- autoload :HasAndBelongsToMany, 'active_record/associations/preloader/has_and_belongs_to_many'
autoload :BelongsTo, 'active_record/associations/preloader/belongs_to'
end
@@ -205,8 +204,6 @@ def preloader_for(reflection, owners, rhs_klass)
reflection.options[:through] ? HasManyThrough : HasMany
when :has_one
reflection.options[:through] ? HasOneThrough : HasOne
- when :has_and_belongs_to_many
- HasAndBelongsToMany
when :belongs_to
BelongsTo
end
View
71 activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb
@@ -1,71 +0,0 @@
-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 = query_scope ids
- 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 load_slices(slices)
- identity_map = {}
- caster = nil
- name = association_key_name
-
- records_to_keys = slices.flat_map { |slice|
- records = records_for(slice)
- caster ||= records.column_types.fetch(name, records.identity_type)
- records.map! { |row|
- record = identity_map[row[klass.primary_key]] ||= klass.instantiate(row)
- [record, caster.type_cast(row[name])]
- }
- }
- @preloaded_records = records_to_keys.map(&:first)
-
- records_to_keys
- 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

0 comments on commit a03ea3f

Please sign in to comment.