From cb670c5c98f535cdf444bb6a8b3fa4fad1fbb362 Mon Sep 17 00:00:00 2001 From: Eugene Kenny Date: Mon, 17 May 2021 21:34:41 +0100 Subject: [PATCH] Avoid running async queries on null relations Calling `load_async` on a null relation was executing the fallback `WHERE 1=0` query, since it doesn't go through `exec_queries`. Since 2a90104989b9f1653337d08871610d394b2d626d, `exec_queries` is implemented in terms of `exec_main_query`, so we only need to override that one method. We can also stop assigning `@records`, as that now happens in `load` since 847643a55acf9c82d2b70448cfa527fee63e9601. --- activerecord/lib/active_record/null_relation.rb | 4 ++-- activerecord/test/cases/null_relation_test.rb | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/null_relation.rb b/activerecord/lib/active_record/null_relation.rb index 00772bf1ec..ff177fb386 100644 --- a/activerecord/lib/active_record/null_relation.rb +++ b/activerecord/lib/active_record/null_relation.rb @@ -56,8 +56,8 @@ module ActiveRecord end private - def exec_queries - @records = [].freeze + def exec_main_query(async: false) + [].freeze end end end diff --git a/activerecord/test/cases/null_relation_test.rb b/activerecord/test/cases/null_relation_test.rb index ef593497f3..a3d0e9cf4e 100644 --- a/activerecord/test/cases/null_relation_test.rb +++ b/activerecord/test/cases/null_relation_test.rb @@ -28,6 +28,12 @@ class NullRelationTest < ActiveRecord::TestCase end end + def test_async_query_on_null_relation + assert_no_queries do + assert_equal [], Developer.none.load_async.load + end + end + def test_none_chained_to_methods_firing_queries_straight_to_db assert_no_queries do assert_equal [], Developer.none.pluck(:id, :name)