Fix N+1 queries on identity in UsersController and BoardsController (#2365)
* Fix N+1 queries on identity in UsersController and BoardsController - Add .includes(:identity) to UsersController#index - Add .includes(creator: :identity) to BoardsController#index - Add N+1 regression tests for both controllers * Simplify controller N+1 tests with query assertions --------- Co-authored-by: Mike Dalessio <mike@37signals.com>
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user