aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorLauro Caetano <laurocaetano1@gmail.com>2014-04-23 23:51:00 -0300
committerLauro Caetano <laurocaetano1@gmail.com>2014-05-20 23:26:51 -0300
commit4debc86bd329d31360272ed15459cde3b9af3a10 (patch)
tree2743821dedf9539da64989fc9e9e79b4e1405d75 /activerecord/lib
parentb17f6e6877162648936c32e62c58305861beaf70 (diff)
downloadrails-4debc86bd329d31360272ed15459cde3b9af3a10.tar.gz
rails-4debc86bd329d31360272ed15459cde3b9af3a10.tar.bz2
rails-4debc86bd329d31360272ed15459cde3b9af3a10.zip
Fix polymorphic eager load with foreign_key as String.
The foreign_key could be `String` and just doing `owners_map[owner_key]` could return `nil`. To prevent this bug, we should `to_s` both keys if their types are different. Fixes #14734.
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/associations/preloader/association.rb19
1 files changed, 17 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/associations/preloader/association.rb b/activerecord/lib/active_record/associations/preloader/association.rb
index bf461070e0..5a172a417d 100644
--- a/activerecord/lib/active_record/associations/preloader/association.rb
+++ b/activerecord/lib/active_record/associations/preloader/association.rb
@@ -58,7 +58,11 @@ module ActiveRecord
def owners_by_key
@owners_by_key ||= owners.group_by do |owner|
- owner[owner_key_name]
+ if owner_key_type == association_key_type
+ owner[owner_key_name]
+ else
+ owner[owner_key_name].to_s
+ end
end
end
@@ -93,13 +97,24 @@ module ActiveRecord
records_by_owner
end
+ def association_key_type
+ @klass.column_types[association_key_name.to_s].type
+ end
+
+ def owner_key_type
+ @model.column_types[owner_key_name.to_s].type
+ end
+
def load_slices(slices)
@preloaded_records = slices.flat_map { |slice|
records_for(slice)
}
@preloaded_records.map { |record|
- [record, record[association_key_name]]
+ key = record[association_key_name]
+ key = key.to_s unless owner_key_type == association_key_type
+
+ [record, key]
}
end