diff --git a/app/controllers/boards_controller.rb b/app/controllers/boards_controller.rb index 191bb72fde..604938deb1 100644 --- a/app/controllers/boards_controller.rb +++ b/app/controllers/boards_controller.rb @@ -5,7 +5,7 @@ class BoardsController < ApplicationController before_action :ensure_permission_to_admin_board, only: %i[ update destroy ] def index - set_page_and_extract_portion_from Current.user.boards.ordered_by_recently_accessed + set_page_and_extract_portion_from Current.user.boards.ordered_by_recently_accessed.includes(creator: :identity) fresh_when etag: @page.records end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 54c18704c5..7062b85e55 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -3,7 +3,7 @@ class UsersController < ApplicationController before_action :ensure_permission_to_change_user, only: %i[ update destroy ] def index - set_page_and_extract_portion_from Current.account.users.active.alphabetically + set_page_and_extract_portion_from Current.account.users.active.alphabetically.includes(:identity) end def show diff --git a/test/controllers/boards_controller_test.rb b/test/controllers/boards_controller_test.rb index 8a426f00ec..6c171b6b2b 100644 --- a/test/controllers/boards_controller_test.rb +++ b/test/controllers/boards_controller_test.rb @@ -278,6 +278,20 @@ class BoardsControllerTest < ActionDispatch::IntegrationTest assert_response :no_content end + test "index avoids N+1 queries on creator and identity" do + assert_queries_match(/FROM [`"]users[`"].* IN \(/, count: 1) do + assert_queries_match(/FROM [`"]identities[`"].* IN \(/, count: 1) do + get boards_path, as: :json + assert_response :success + end + end + + json = @response.parsed_body + first_board = json.first + assert first_board["creator"].present? + assert first_board["creator"]["email_address"].present? + end + private def next_page_from_link_header(link_header) url = link_header&.match(/<([^>]+)>;\s*rel="next"/)&.captures&.first diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb index ab91a4ab66..094a2310cf 100644 --- a/test/controllers/users_controller_test.rb +++ b/test/controllers/users_controller_test.rb @@ -144,4 +144,16 @@ class UsersControllerTest < ActionDispatch::IntegrationTest assert_response :no_content end + + test "index avoids N+1 queries on identity" do + sign_in_as :kevin + + assert_queries_match(/FROM [`"]identities[`"].* IN \(/, count: 1) do + get users_path, as: :json + assert_response :success + end + + json = @response.parsed_body + assert json.first["email_address"].present? + end end