Skip to content

Commit 605d910

Browse files
nvasilevskicbelan
andcommitted
Fix eager loading of composite primary key associations
Co-Authored-By: Christine Belan <[email protected]>
1 parent fe81d66 commit 605d910

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

activerecord/lib/active_record/associations/join_dependency.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,10 @@ def construct(ar_parent, parent, row, seen, model_cache, strict_loading_value)
254254

255255
if node.primary_key
256256
keys = Array(node.primary_key).map { |column| aliases.column_alias(node, column) }
257-
ids = keys.map { |key| row[key] }
257+
id = keys.map { |key| row[key] }
258258
else
259259
keys = Array(node.reflection.join_primary_key).map { |column| aliases.column_alias(node, column.to_s) }
260-
ids = keys.map { nil } # Avoid id-based model caching.
260+
id = keys.map { nil } # Avoid id-based model caching.
261261
end
262262

263263
if keys.any? { |key| row[key].nil? }
@@ -266,11 +266,9 @@ def construct(ar_parent, parent, row, seen, model_cache, strict_loading_value)
266266
next
267267
end
268268

269-
ids.each do |id|
270-
unless model = seen[ar_parent][node][id]
271-
model = construct_model(ar_parent, node, row, model_cache, id, strict_loading_value)
272-
seen[ar_parent][node][id] = model if id
273-
end
269+
unless model = seen[ar_parent][node][id]
270+
model = construct_model(ar_parent, node, row, model_cache, id, strict_loading_value)
271+
seen[ar_parent][node][id] = model if id
274272
end
275273

276274
construct(model, node, row, seen, model_cache, strict_loading_value)

activerecord/test/cases/associations/eager_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,6 +1729,26 @@ def test_preloading_has_many_through_with_custom_scope
17291729
assert_equal(expected_tag_ids.sort, blog_post.tags.map(&:id).sort)
17301730
end
17311731

1732+
test "preloading belongs_to CPK model with one of the keys being shared between models" do
1733+
post1 = Cpk::Post.create!(title: "post1", author: "the_same_author")
1734+
Cpk::Comment.create!(post: post1, text: "great post1!")
1735+
1736+
post2 = Cpk::Post.create!(title: "post2", author: "the_same_author")
1737+
Cpk::Comment.create!(post: post2, text: "great post2!")
1738+
1739+
comments = Cpk::Comment.eager_load(:post).to_a
1740+
expected = {
1741+
"great post1!" => "post1",
1742+
"great post2!" => "post2"
1743+
}
1744+
1745+
actual = comments.each_with_object({}) do |comment, hash|
1746+
hash[comment.text] = comment.post.title
1747+
end
1748+
1749+
assert_equal expected, actual
1750+
end
1751+
17321752
test "preloading belongs_to with cpk" do
17331753
order = Cpk::Order.create!(shop_id: 2)
17341754
order_agreement = Cpk::OrderAgreement.create!(order: order)

activerecord/test/models/cpk/comment.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ module Cpk
44
class Comment < ActiveRecord::Base
55
self.table_name = :cpk_comments
66
belongs_to :commentable, class_name: "Cpk::Post", query_constraints: %i[commentable_title commentable_author], polymorphic: true
7+
belongs_to :post, class_name: "Cpk::Post", query_constraints: %i[commentable_title commentable_author]
78
end
89
end

activerecord/test/schema/schema.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@
270270
t.string :commentable_title
271271
t.string :commentable_author
272272
t.string :commentable_type
273+
t.text :text
273274
end
274275

275276
create_table :cpk_reviews, force: true do |t|

0 commit comments

Comments
 (0)