Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't eager load when plucking with includes/limit #36273

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/relation/calculations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def pluck(*column_names)
end

if has_include?(column_names.first)
relation = apply_join_dependency
relation = apply_join_dependency(eager_loading: false)
relation.pluck(*column_names)
else
klass.disallow_raw_sql!(column_names)
Expand Down
12 changes: 12 additions & 0 deletions activerecord/test/cases/calculations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,18 @@ def test_pluck_with_includes_offset
assert_equal [], Topic.includes(:replies).order(:id).offset(5).pluck(:id)
end

def test_pluck_with_includes_limit
c = Company.create!(name: "test", contracts: [Contract.new(developer_id: 7), Contract.new(developer_id: 8)])
assert_equal [c.id], Company.where(id: c.id).includes(:contracts).limit(1).pluck(:id)
assert_equal [7], Company.where(id: c.id).includes(:contracts).limit(1).pluck(:'contracts.developer_id')
end

def test_pluck_same_as_outer_join
c = Company.create!(name: "test", contracts: [Contract.new(developer_id: 7), Contract.new(developer_id: 8)])
assert_equal Company.where(id: c.id).includes(:contracts).limit(1).pluck(:'contracts.developer_id'),
Company.where(id: c.id).left_outer_joins(:contracts).limit(1).pluck(:'contracts.developer_id')
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this test is needed or useful, so I'm happy to remove it if desired.


def test_pluck_with_join
assert_equal [[2, 2], [4, 4]], Reply.includes(:topic).pluck(:id, :"topics.id")
end
Expand Down