diff options
-rw-r--r-- | activerecord/lib/active_record/association_preload.rb | 6 |
1 files changed, 3 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/association_preload.rb b/activerecord/lib/active_record/association_preload.rb index a305551093..ba225b700c 100644 --- a/activerecord/lib/active_record/association_preload.rb +++ b/activerecord/lib/active_record/association_preload.rb @@ -336,11 +336,11 @@ module ActiveRecord end end else - id_map = {} - records.each do |record| + id_map = records.group_by do |record| key = record.send(primary_key_name) - (id_map[key.to_s] ||= []) << record if key + key && key.to_s end + id_map.delete nil klasses_and_ids[reflection.klass.name] = id_map unless id_map.empty? end |