File tree Expand file tree Collapse file tree 2 files changed +14
-1
lines changed
lib/active_record/relation Expand file tree Collapse file tree 2 files changed +14
-1
lines changed Original file line number Diff line number Diff line change @@ -315,7 +315,7 @@ def pluck(*column_names)
315
315
columns = relation . arel_columns ( column_names )
316
316
relation . select_values = columns
317
317
result = skip_query_cache_if_necessary do
318
- if where_clause . contradiction?
318
+ if where_clause . contradiction? && ! possible_aggregation? ( column_names )
319
319
ActiveRecord ::Result . empty ( async : @async )
320
320
else
321
321
model . with_connection do |c |
@@ -450,6 +450,13 @@ def distinct_select?(column_name)
450
450
column_name . is_a? ( ::String ) && /\b DISTINCT[\s (]/i . match? ( column_name )
451
451
end
452
452
453
+ def possible_aggregation? ( column_names )
454
+ column_names . any? do |column_name |
455
+ Arel . arel_node? ( column_name ) ||
456
+ ( column_name . is_a? ( String ) && column_name . include? ( "(" ) )
457
+ end
458
+ end
459
+
453
460
def aggregate_column ( column_name )
454
461
case column_name
455
462
when Arel ::Expressions
Original file line number Diff line number Diff line change @@ -445,6 +445,12 @@ def test_skip_preloading_after_arel_has_been_generated
445
445
end
446
446
end
447
447
448
+ test "runs queries when using pick with expression column and empty IN" do
449
+ assert_queries_count ( 1 ) do
450
+ assert_equal 0 , Post . where ( id : [ ] ) . pick ( Arel . sql ( "COUNT(*)" ) )
451
+ end
452
+ end
453
+
448
454
test "can unscope empty IN" do
449
455
assert_queries_count ( 1 ) do
450
456
Post . where ( id : [ ] ) . unscope ( where : :id ) . load
You can’t perform that action at this time.
0 commit comments