From 4f3a696ea7896ca9de46ada4bf0b4b7786c48131 Mon Sep 17 00:00:00 2001
From: daiao <358551898@qq.com>
Date: Wed, 10 Jul 2019 09:48:26 +0800
Subject: [PATCH] =?UTF-8?q?=E6=89=93=E6=98=9F=E6=98=9F=E4=BB=A3=E7=A0=81?=
 =?UTF-8?q?=E4=BC=98=E5=8C=96?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 app/models/shixun.rb | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

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]