diff --git a/app/models/shixun.rb b/app/models/shixun.rb index c4345f90a..4a4aea355 100644 --- a/app/models/shixun.rb +++ b/app/models/shixun.rb @@ -158,13 +158,12 @@ class Shixun < ApplicationRecord # 实训评分信息 # return [实训评分, 5星评分比例, 4星评分比例, 3星评分比例, 2星评分比例, 1星评分比例] def shixun_preference_info - game_star_info = Game.find_by_sql("select g.star from - (games g left join (myshixuns m join shixuns s on s.id = m.shixun_id) on m.id = g.myshixun_id) - where g.star != 0 and s.id = #{self.id}") + game_star_info = Game.joins(challenge: :shixun).where(shixuns: {id: id}).where.not(games: {star: 0}).pluck(:star) + star_info = [] if game_star_info.present? 5.downto(1) do |i| - star_info << ((game_star_info.select{|s| s.star == i}.count / game_star_info.count.to_f) * 100).round + star_info << ((game_star_info.select{|s| s == i}.count / game_star_info.count.to_f) * 100).round end sum = star_info.sum max = star_info.max @@ -175,7 +174,7 @@ class Shixun < ApplicationRecord star_info = star_info.map{|s| s == max ? s + 1 : s} end cnt = game_star_info.count - sum = game_star_info.sum(&:star) + sum = game_star_info.sum star_info.unshift((sum / cnt.to_f).round(1)) else star_info = [5.0, 100, 0, 0, 0, 0]