aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb')
-rw-r--r--activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb24
1 files changed, 15 insertions, 9 deletions
diff --git a/activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb b/activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb
index c042a44b21..b62ca6f681 100644
--- a/activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb
+++ b/activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb
@@ -33,16 +33,22 @@ module ActiveRecord
# 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 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)
- def type_caster(results, name)
- caster = results.column_types.fetch(name, results.identity_type)
- lambda { |value| caster.type_cast value }
+ records_to_keys
end
def build_scope