Correctly find nested association reflections for #37356

Follow up of #37434.
This commit is contained in:
Ryuta Kamizono 2019-10-11 16:20:11 +09:00
parent 08df451206
commit 75576b52ee
3 changed files with 29 additions and 7 deletions

@ -384,8 +384,16 @@ def apply_join_dependency(eager_loading: group_values.empty?)
)
relation = except(:includes, :eager_load, :preload).joins!(join_dependency)
reflections = join_dependency.reflections + joins_values.map { |joins_value| reflect_on_association(joins_value) }.reject(&:blank?)
if eager_loading && !using_limitable_reflections?(reflections)
if eager_loading && !(
using_limitable_reflections?(join_dependency.reflections) &&
using_limitable_reflections?(
construct_join_dependency(
select_association_list(joins_values).concat(
select_association_list(left_outer_joins_values)
), nil
).reflections
)
)
if has_limit_or_offset?
limited_ids = limited_ids_for(relation)
limited_ids.empty? ? relation.none! : relation.where!(primary_key => limited_ids)

@ -1083,15 +1083,23 @@ def build_from
end
end
def valid_association_list(associations)
def select_association_list(associations)
result = []
associations.each do |association|
case association
when Hash, Symbol, Array
# valid
result << association
else
raise ArgumentError, "only Hash, Symbol and Array are allowed"
yield if block_given?
end
end
result
end
def valid_association_list(associations)
select_association_list(associations) do
raise ArgumentError, "only Hash, Symbol and Array are allowed"
end
end
def build_left_outer_joins(manager, outer_joins, aliases)

@ -1337,9 +1337,15 @@ def test_with_limiting_with_custom_select
end
def test_eager_load_for_no_has_many_with_limit_and_joins_for_has_many
relation = Post.eager_load(:author).joins(:comments)
relation = Post.eager_load(:author).joins(comments: :post)
assert_equal 5, relation.to_a.size
assert_equal relation.limit(5).to_a.size, relation.to_a.size
assert_equal 5, relation.limit(5).to_a.size
end
def test_eager_load_for_no_has_many_with_limit_and_left_joins_for_has_many
relation = Post.eager_load(:author).left_joins(comments: :post)
assert_equal 11, relation.to_a.size
assert_equal 11, relation.limit(11).to_a.size
end
def test_find_one_message_on_primary_key