Skip to content

Commit 056f76c

Browse files
committed
Correctly find nested association reflections for rails#37356
Follow up of rails#37434.
1 parent 953543a commit 056f76c

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

activerecord/lib/active_record/relation/finder_methods.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,16 @@ def apply_join_dependency(eager_loading: group_values.empty?)
376376
)
377377
relation = except(:includes, :eager_load, :preload).joins!(join_dependency)
378378

379-
reflections = join_dependency.reflections + joins_values.map { |joins_value| reflect_on_association(joins_value) }.reject(&:blank?)
380-
if eager_loading && !using_limitable_reflections?(reflections)
379+
if eager_loading && !(
380+
using_limitable_reflections?(join_dependency.reflections) &&
381+
using_limitable_reflections?(
382+
construct_join_dependency(
383+
select_association_list(joins_values).concat(
384+
select_association_list(left_outer_joins_values)
385+
), nil
386+
).reflections
387+
)
388+
)
381389
if has_limit_or_offset?
382390
limited_ids = limited_ids_for(relation)
383391
limited_ids.empty? ? relation.none! : relation.where!(primary_key => limited_ids)

activerecord/lib/active_record/relation/query_methods.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,15 +1083,23 @@ def build_from
10831083
end
10841084
end
10851085

1086-
def valid_association_list(associations)
1086+
def select_association_list(associations)
1087+
result = []
10871088
associations.each do |association|
10881089
case association
10891090
when Hash, Symbol, Array
1090-
# valid
1091+
result << association
10911092
else
1092-
raise ArgumentError, "only Hash, Symbol and Array are allowed"
1093+
yield if block_given?
10931094
end
10941095
end
1096+
result
1097+
end
1098+
1099+
def valid_association_list(associations)
1100+
select_association_list(associations) do
1101+
raise ArgumentError, "only Hash, Symbol and Array are allowed"
1102+
end
10951103
end
10961104

10971105
def build_left_outer_joins(manager, outer_joins, aliases)

activerecord/test/cases/finder_test.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,9 +1330,15 @@ def test_with_limiting_with_custom_select
13301330
end
13311331

13321332
def test_eager_load_for_no_has_many_with_limit_and_joins_for_has_many
1333-
relation = Post.eager_load(:author).joins(:comments)
1333+
relation = Post.eager_load(:author).joins(comments: :post)
13341334
assert_equal 5, relation.to_a.size
1335-
assert_equal relation.limit(5).to_a.size, relation.to_a.size
1335+
assert_equal 5, relation.limit(5).to_a.size
1336+
end
1337+
1338+
def test_eager_load_for_no_has_many_with_limit_and_left_joins_for_has_many
1339+
relation = Post.eager_load(:author).left_joins(comments: :post)
1340+
assert_equal 11, relation.to_a.size
1341+
assert_equal 11, relation.limit(11).to_a.size
13361342
end
13371343

13381344
def test_find_one_message_on_primary_key

0 commit comments

Comments
 (0)