PERF: Preload user options when status is enabled (#28827)

The user option 'hide_profile_and_presence' is necessary to figure out
if the user status has to be displayed or not. In order to avoid N+1s
generated by `include_status?` method, both `user_status` and
`user_option` relations have to be included.
This commit is contained in:
Bianca Nenciu
2024-09-11 10:39:14 +03:00
committed by GitHub
parent d63ffe22f4
commit 38592dc48e
2 changed files with 22 additions and 2 deletions

View File

@ -752,8 +752,9 @@ class TopicView
usernames.flatten!
usernames.uniq!
users =
User.where(username_lower: usernames).includes(:user_status).index_by(&:username_lower)
users = User.where(username_lower: usernames)
users = users.includes(:user_option, :user_status) if SiteSetting.enable_user_status
users = users.index_by(&:username_lower)
mentions.reduce({}) do |hash, (post_id, post_mentioned_usernames)|
hash[post_id] = post_mentioned_usernames.map { |username| users[username] }.compact