From 8e8877b408961ba79b2d35ff448742b439892143 Mon Sep 17 00:00:00 2001 From: Kevin Gilpin Date: Tue, 21 Jul 2020 16:02:18 -0400 Subject: [PATCH 1/3] Eager-load the user feed to avoid repetitive queries --- app/models/user.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/models/user.rb b/app/models/user.rb index ac83c4d70..23b06ba4f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -83,6 +83,8 @@ def feed WHERE follower_id = :user_id" Micropost.where("user_id IN (#{following_ids}) OR user_id = :user_id", user_id: id) + .includes(image_attachment: :blob) + .includes(:user) end # Follows a user. From 8220f4a43d044b9b7d149352d44d98f68acf4288 Mon Sep 17 00:00:00 2001 From: Kevin Gilpin Date: Fri, 24 Jul 2020 15:34:10 -0400 Subject: [PATCH 2/3] Add both eager-load relations on the same line --- app/models/user.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 23b06ba4f..36f43dc55 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -83,8 +83,7 @@ def feed WHERE follower_id = :user_id" Micropost.where("user_id IN (#{following_ids}) OR user_id = :user_id", user_id: id) - .includes(image_attachment: :blob) - .includes(:user) + .includes(:user, image_attachment: :blob) end # Follows a user. From 7d37dc23f3fcbaac2f51fd06072bf3ff2dbc8c1a Mon Sep 17 00:00:00 2001 From: Kevin Gilpin Date: Fri, 24 Jul 2020 15:34:32 -0400 Subject: [PATCH 3/3] Print the number of queries that occur in a request --- test/integration/microposts_interface_test.rb | 39 +++++++++++++++++-- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/test/integration/microposts_interface_test.rb b/test/integration/microposts_interface_test.rb index b90cffaa5..c76662363 100644 --- a/test/integration/microposts_interface_test.rb +++ b/test/integration/microposts_interface_test.rb @@ -2,10 +2,35 @@ class MicropostsInterfaceTest < ActionDispatch::IntegrationTest + class QueryCountHandler + attr_reader :count + + def initialize + @count = 0 + end + + def call(name, started, finished, unique_id, payload) + # puts payload[:sql] + @count += 1 + end + end + def setup @user = users(:michael) end + def count_sql(message) + warn "Counting SQL queries for: #{message}" + + count_handler = QueryCountHandler.new + ActiveSupport::Notifications.subscribe 'sql.active_record', count_handler + + yield + + warn "Observed #{count_handler.count} SQL queries" + ActiveSupport::Notifications.unsubscribe count_handler + end + test "micropost interface" do log_in_as(@user) get root_path @@ -18,12 +43,18 @@ def setup # Valid submission content = "This micropost really ties the room together" image = fixture_file_upload('kitten.jpg', 'image/jpeg') + assert_difference 'Micropost.count', 1 do - post microposts_path, params: { micropost: { content: content, - image: image } } + count_sql 'Create a valid post' do + post microposts_path, params: { micropost: { content: content, + image: image } } + end + assert assigns(:micropost).image.attached? + end + + count_sql 'Follow redirect after posting' do + follow_redirect! end - assert assigns(:micropost).image.attached? - follow_redirect! assert_match content, response.body # Delete a post. assert_select 'a', 'delete'