Skip to content

Commit

Permalink
Avoid making query when using where(attr: []) for pluck
Browse files Browse the repository at this point in the history
Follow up #37266.
  • Loading branch information
kamipo committed Jan 27, 2020
1 parent 104c184 commit 0d0c30e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
8 changes: 7 additions & 1 deletion activerecord/lib/active_record/relation/calculations.rb
Expand Up @@ -190,7 +190,13 @@ def pluck(*column_names)
klass.disallow_raw_sql!(column_names)
relation = spawn
relation.select_values = column_names
result = skip_query_cache_if_necessary { klass.connection.select_all(relation.arel, nil) }
result = skip_query_cache_if_necessary do
if where_clause.contradiction?
ActiveRecord::Result.new([], [])
else
klass.connection.select_all(relation.arel, nil)
end
end
result.cast_values(klass.attribute_types)
end
end
Expand Down
7 changes: 7 additions & 0 deletions activerecord/test/cases/calculations_test.rb
Expand Up @@ -678,6 +678,13 @@ def test_pluck
assert_equal [1, 2, 3, 4, 5], Topic.order(:id).pluck(:id)
end

def test_pluck_with_empty_in
Topic.send(:load_schema)
assert_no_queries do
assert_equal [], Topic.where(id: []).pluck(:id)
end
end

def test_pluck_without_column_names
if current_adapter?(:OracleAdapter)
assert_equal [[1, "Firm", 1, nil, "37signals", nil, 1, nil, nil]], Company.order(:id).limit(1).pluck
Expand Down

0 comments on commit 0d0c30e

Please sign in to comment.