diff --git a/app/models/team_bot_installation.rb b/app/models/team_bot_installation.rb index 8227d11c2..e359f43cc 100644 --- a/app/models/team_bot_installation.rb +++ b/app/models/team_bot_installation.rb @@ -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 diff --git a/test/models/team_bot_installation_test.rb b/test/models/team_bot_installation_test.rb index d55e5d075..04bc6ba48 100644 --- a/test/models/team_bot_installation_test.rb +++ b/test/models/team_bot_installation_test.rb @@ -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