fix user shixuns list filter bug and n+1 bug

dev_course
p31729568 6 years ago
parent 2420ce6a1b
commit 7ddd0c4f5d

@ -4,6 +4,10 @@ class Users::ShixunsController < Users::BaseController
@count = shixuns.count
@shixuns = paginate(shixuns.includes(:first_tag_repertoire), special: true)
ids = @shixuns.map(&:id)
@finished_challenges_count_map = Game.joins(:myshixun).where(user_id: observed_user.id, status: 2)
.where(myshixuns: { shixun_id: ids }).group('myshixuns.shixun_id').count
end
private

@ -1,8 +1,4 @@
module ShixunDecorator
def finished_challenges_count(user)
Game.joins(:myshixun).where(user_id: user.id, status: 2, myshixuns: { shixun_id: id }).count
end
def human_status
I18n.t("shixun.status.#{status}")
end

@ -233,4 +233,8 @@ class Shixun < ApplicationRecord
shixun_members.where(role: [1, 2]).exists?(user_id: user.id)
end
def finished_challenges_count(user)
Game.joins(:myshixun).where(user_id: user.id, status: 2, myshixuns: { shixun_id: id }).count
end
end

@ -58,7 +58,7 @@ class Users::ShixunService
when 'passed' then 1
when 'processing' then 0
end
relations.where(myshixuns: { status: status }) if status
relations = relations.where(myshixuns: { status: status }) if status
relations
end

@ -6,4 +6,4 @@ json.name shixun.name
json.status shixun.status
json.human_status shixun.human_status
json.challenges_count shixun.challenges_count
json.finished_challenges_count shixun.finished_challenges_count(user)
json.finished_challenges_count @finished_challenges_count_map&.fetch(shixun.id, 0) || shixun.finished_challenges_count(user)
Loading…
Cancel
Save