diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2013-09-24 17:21:19 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2013-09-24 17:21:19 -0700 |
commit | 4c045876da051694aa4b9c69dd708283c5e2879d (patch) | |
tree | 9382faba277836e03fdb98ba87bc6ab638d9a5f0 /activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb | |
parent | c4e1fdd9b20020594135aaf5ae06ec1bec05df03 (diff) | |
download | rails-4c045876da051694aa4b9c69dd708283c5e2879d.tar.gz rails-4c045876da051694aa4b9c69dd708283c5e2879d.tar.bz2 rails-4c045876da051694aa4b9c69dd708283c5e2879d.zip |
push slice loading to it's own method so we can remove the type casting
code
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.rb | 23 |
1 files changed, 12 insertions, 11 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 1c119d739f..996575f5aa 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,18 +33,19 @@ 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(preloader) - return @associated_records_by_owner if @associated_records_by_owner + def load_slices(slices) + identity_map = {} + caster = nil + name = association_key_name - records = {} - @associated_records_by_owner = super.each_value do |rows| - rows.map! { |row| records[row[klass.primary_key]] ||= klass.instantiate(row) } - end - end - - def set_type_caster(results, name) - caster = results.column_types.fetch(name, results.identity_type) - @type_caster = lambda { |value| caster.type_cast value } + 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])] + } + } end def build_scope |