Skip to content

Commit

Permalink
Fix N+1 query, after we removed second_level_cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Oct 29, 2024
1 parent ee96762 commit bdb99d2
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/controllers/admin/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Admin
class UsersController < Admin::ApplicationController
def index
scope = User.all
scope = User.all.includes(:profile)
scope = scope.where(type: params[:type]) if params[:type].present?
scope = scope.where(state: params[:state]) if params[:state].present?
field = params[:field] || "login"
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/topics/list_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def topics_scope(base_scope = Topic, without_nodes: true)
end

# must include :user, because it's uses for _topic.html.erb fragment cache_key
scope.includes(:user)
scope.includes(:user, :node, :last_reply_user)
end
end
end
12 changes: 6 additions & 6 deletions app/controllers/topics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class TopicsController < ApplicationController
def index
@suggest_topics = []
if params[:page].to_i <= 1
@suggest_topics = topics_scope.suggest.includes(:node).limit(3)
@suggest_topics = topics_scope.suggest.limit(3)
end
@topics = topics_scope.without_suggest.last_actived.includes(:node).page(params[:page])
@topics = topics_scope.without_suggest.last_actived.page(params[:page])
@page_title = t("menu.topics")
@read_topic_ids = []
if current_user
Expand All @@ -21,7 +21,7 @@ def index
end

def feed
@topics = Topic.recent.without_ban.without_hide_nodes.includes(:node, :user, :last_reply_user).limit(20)
@topics = Topic.recent.without_ban.without_hide_nodes.limit(20)
render layout: false if stale?(@topics)
end

Expand All @@ -35,19 +35,19 @@ def node

def node_feed
@node = Node.find(params[:id])
@topics = @node.topics.recent.limit(20)
@topics = @node.topics.includes(:user, :node, :last_reply_user).recent.limit(20)
render layout: false
end

def show
@topic = Topic.unscoped.includes(:user).find(params[:id])
@topic = Topic.unscoped.includes(:user, :last_reply_user).find(params[:id])
render_404 if @topic.deleted?

@node = @topic.node
@show_raw = params[:raw] == "1"
@can_reply = can?(:create, Reply)

@replies = Reply.unscoped.where(topic_id: @topic.id).order(:id).all
@replies = Reply.unscoped.where(topic_id: @topic.id).includes(:user).order(:id).all
@user_like_reply_ids = current_user&.like_reply_ids_by_replies(@replies) || []

@has_followed = current_user&.follow_topic?(@topic)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/users/user_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def only_user!

def user_show
@replies = @user.replies.without_system.fields_for_list.recent.includes(:topic).limit(10)
@topics = @user.topics.fields_for_list.high_likes.page(params[:page])
@topics = @user.topics.fields_for_list.high_likes.includes(:node).page(params[:page])
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def index
end

def feed
@topics = @user.topics.recent.limit(20)
@topics = @user.topics.includes(:node).recent.limit(20)
end

def city
Expand Down

0 comments on commit bdb99d2

Please sign in to comment.