Merge pull request #50325 from fatkodima/fix-preloader-with-loaded-association-with-cpk

Fix `Preloader` to not generate a query for already loaded association with `query_constraints`
This commit is contained in:
Eileen M. Uchitelle 2023-12-11 09:28:10 -05:00 committed by GitHub
commit b02ebb30a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 0 deletions

View File

@ -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 }

View File

@ -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)