diff options
author | Yves Senn <yves.senn@gmail.com> | 2014-05-21 08:19:01 +0200 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2014-05-21 08:51:32 +0200 |
commit | f69f20e4ff6295eab6aa5c38eecb2489d9a90676 (patch) | |
tree | 2c0ebc9cfedf5e193616099e15acfe9bf99344c2 /activerecord/lib/active_record/associations | |
parent | b17f6e6877162648936c32e62c58305861beaf70 (diff) | |
parent | 4debc86bd329d31360272ed15459cde3b9af3a10 (diff) | |
download | rails-f69f20e4ff6295eab6aa5c38eecb2489d9a90676.tar.gz rails-f69f20e4ff6295eab6aa5c38eecb2489d9a90676.tar.bz2 rails-f69f20e4ff6295eab6aa5c38eecb2489d9a90676.zip |
Merge pull request #14855 from laurocaetano/fix_polymorphic_with_string_key
Fix polymorphic eager load with foreign_key as String.
Diffstat (limited to 'activerecord/lib/active_record/associations')
-rw-r--r-- | activerecord/lib/active_record/associations/preloader/association.rb | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/associations/preloader/association.rb b/activerecord/lib/active_record/associations/preloader/association.rb index bf461070e0..63773bd5e1 100644 --- a/activerecord/lib/active_record/associations/preloader/association.rb +++ b/activerecord/lib/active_record/associations/preloader/association.rb @@ -57,9 +57,15 @@ module ActiveRecord end def owners_by_key - @owners_by_key ||= owners.group_by do |owner| - owner[owner_key_name] - end + @owners_by_key ||= if key_conversion_required? + owners.group_by do |owner| + owner[owner_key_name].to_s + end + else + owners.group_by do |owner| + owner[owner_key_name] + end + end end def options @@ -93,13 +99,28 @@ module ActiveRecord records_by_owner end + def key_conversion_required? + association_key_type != owner_key_type + 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 if key_conversion_required? + + [record, key] } end |