Merge pull request #37434 from giraffate/fix_eager_loading_with_limit_and_join_has_many

Fix eager load for no :has_many with limit and joins for :has_many
This commit is contained in:
Ryuta Kamizono 2019-10-11 15:21:25 +09:00 committed by GitHub
commit 08df451206
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 1 deletions

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

@ -1336,6 +1336,12 @@ def test_with_limiting_with_custom_select
assert_equal [0, 1, 1], posts.map(&:author_id).sort
end
def test_eager_load_for_no_has_many_with_limit_and_joins_for_has_many
relation = Post.eager_load(:author).joins(:comments)
assert_equal 5, relation.to_a.size
assert_equal relation.limit(5).to_a.size, relation.to_a.size
end
def test_find_one_message_on_primary_key
e = assert_raises(ActiveRecord::RecordNotFound) do
Car.find(0)