Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix N+1 query issue in bot_user method by using find_by #2164

Merged
merged 2 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/team_bot_installation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def alegre_settings
end

def bot_user
BotUser.where(id: self.user_id).last
@bot_user ||= BotUser.find_by(id: self.user_id)
end

def apply_default_settings
Expand Down
13 changes: 13 additions & 0 deletions test/models/team_bot_installation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,17 @@ def setup
assert_equal 'def456', tbi.reload.get_turnio_token
end
end

test "should not trigger additional queries when accessing bot_user" do
team_bot = create_team_bot set_approved: true
team_bot_installation = create_team_bot_installation(user_id: team_bot.id)

initial_query_count = ActiveRecord::Base.connection.query_cache.size

assert_queries(0) do
team_bot_installation.bot_user
end

assert_equal initial_query_count, ActiveRecord::Base.connection.query_cache.size
end
end
Loading