diff --git a/activerecord/lib/active_record/associations/preloader/association.rb b/activerecord/lib/active_record/associations/preloader/association.rb index 26e08dba7e0..4db5c67ad0f 100644 --- a/activerecord/lib/active_record/associations/preloader/association.rb +++ b/activerecord/lib/active_record/associations/preloader/association.rb @@ -38,6 +38,8 @@ module ActiveRecord end def load_records_for_keys(keys, &block) + return [] if keys.empty? + if association_key_name.is_a?(Array) query_constraints = Hash.new { |hsh, key| hsh[key] = Set.new } diff --git a/activerecord/test/cases/associations_test.rb b/activerecord/test/cases/associations_test.rb index 1967e0d11d7..b55fb148b68 100644 --- a/activerecord/test/cases/associations_test.rb +++ b/activerecord/test/cases/associations_test.rb @@ -1388,6 +1388,15 @@ class PreloaderTest < ActiveRecord::TestCase assert_equal sharded_blog_posts(:great_post_blog_one), comment.blog_post end + def test_preload_loaded_belongs_to_association_with_composite_foreign_key + comment = sharded_comments(:great_comment_blog_post_one) + comment.blog_post + + assert_no_queries do + ActiveRecord::Associations::Preloader.new(records: [comment], associations: :blog_post).call + end + end + def test_preload_has_many_through_association_with_composite_query_constraints tag = sharded_tags(:short_read_blog_one)