Skip to content

Commit 2edad3c

Browse files
authored
Merge pull request rails#52272 from fatkodima/skip-triggering-instantiation-notification
Skip triggering "instantiation.active_record" notification when there are no records
2 parents 4fa5681 + 2410312 commit 2edad3c

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

activerecord/lib/active_record/querying.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ def _query_by_sql(connection, sql, binds = [], preparable: nil, async: false, al
7171
end
7272

7373
def _load_from_sql(result_set, &block) # :nodoc:
74+
return [] if result_set.empty?
75+
7476
column_types = result_set.column_types
7577

7678
unless column_types.empty?

activerecord/test/cases/instrumentation_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require "cases/helper"
4+
require "models/author"
45
require "models/book"
56
require "models/clothing_item"
67

@@ -170,6 +171,22 @@ def test_payload_connection_with_query_cache_enabled
170171
ensure
171172
ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
172173
end
174+
175+
def test_no_instantiation_notification_when_no_records
176+
author = Author.create!(name: "David")
177+
178+
called = false
179+
subscriber = ActiveSupport::Notifications.subscribe("instantiation.active_record") do
180+
called = true
181+
end
182+
183+
Author.where(id: 0).to_a
184+
author.books.to_a
185+
186+
assert_equal false, called
187+
ensure
188+
ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
189+
end
173190
end
174191

175192
module TransactionInSqlActiveRecordPayloadTests

0 commit comments

Comments
 (0)