From 1c7923e5bdedff26d45b7002a38455c9bdf7fbd1 Mon Sep 17 00:00:00 2001 From: yanxd Date: Thu, 5 Jun 2014 16:53:41 +0800 Subject: [PATCH] sql --- app/controllers/bids_controller.rb | 4 +--- app/helpers/bids_helper.rb | 2 +- app/helpers/courses_helper.rb | 2 +- app/models/user.rb | 2 +- app/views/bids/_homework_list.html.erb | 11 +++-------- app/views/layouts/base_homework.html.erb | 4 ++-- 6 files changed, 9 insertions(+), 16 deletions(-) diff --git a/app/controllers/bids_controller.rb b/app/controllers/bids_controller.rb index c0df0699c..2ad60fc29 100644 --- a/app/controllers/bids_controller.rb +++ b/app/controllers/bids_controller.rb @@ -410,9 +410,7 @@ class BidsController < ApplicationController @homework = HomeworkAttach.new #@homework_list = @bid.homeworks #增加作业按评分排序, - @homework_list = HomeworkAttach.find_by_sql("SELECT homework_attaches.*, - (SELECT AVG(stars) FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches.id) AS score - FROM homework_attaches WHERE bid_id = #{@bid.id} ORDER BY score DESC") + @homework_list = @bid.homeworks.joins(:user).includes(:rate_averages, :user, :attachments).order('seems_rateable_cached_ratings.avg DESC') if params[:student_id].present? @temp = [] @homework_list.each do |pro| diff --git a/app/helpers/bids_helper.rb b/app/helpers/bids_helper.rb index 8b03d74cd..40cec6edb 100644 --- a/app/helpers/bids_helper.rb +++ b/app/helpers/bids_helper.rb @@ -158,7 +158,7 @@ module BidsHelper #当前用户是不是指定课程的学生 def is_cur_course_student? course people = [] - course.members.each do |member| + course.members.includes(:user, :roles).each do |member| if [5,10].include? member.roles.first.id people << member.user end diff --git a/app/helpers/courses_helper.rb b/app/helpers/courses_helper.rb index fb0a269e6..f16aef911 100644 --- a/app/helpers/courses_helper.rb +++ b/app/helpers/courses_helper.rb @@ -139,7 +139,7 @@ module CoursesHelper #判断制定用户是不是当前课程的老师 def is_course_teacher user,course people = [] - course.members.each do |member| + course.members.includes(:roles, :user).each do |member| role_id = member.roles.first.id if TeacherRoles.include? role_id people << member.user diff --git a/app/models/user.rb b/app/models/user.rb index d79cf11e1..689cc7b2e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -137,7 +137,7 @@ class User < Principal has_one :user_extensions,:dependent => :destroy ## end - #default_scope -> { includes(:user_extensions, :user_score) } + default_scope -> { includes(:user_extensions, :user_score) } scope :teacher, -> { joins(:user_extensions).where('user_extensions.identity = ?', UserExtensions::TEACHER) } diff --git a/app/views/bids/_homework_list.html.erb b/app/views/bids/_homework_list.html.erb index 48f1c150d..d2d58753b 100644 --- a/app/views/bids/_homework_list.html.erb +++ b/app/views/bids/_homework_list.html.erb @@ -83,13 +83,8 @@ 作业评分: - <% stars_reates = homework.rates(:quality) %> - <% sum = 0 %> - <% stars_reates.each do |star_reates| %> - <% sum = sum + star_reates.stars %> - <% end %> - <% stars_reates_count = stars_reates.count == 0 ? 1 : stars_reates.count %> - <%= format("%.2f", sum * 1.0 / stars_reates_count) %> + + <%= format("%.2f", homework.rate_averages.first.try(:avg).to_i ) %> <% if Time.parse(@bid.deadline.to_s) < Time.parse(homework.created_at.to_s) %> @@ -100,7 +95,7 @@   <% if User.current.member_of?(@bid.courses.first) %> - <%= l(:label_bidding_user_studentcode) %>  : <%= homework.user.user_extensions.student_id%> + <%= l(:label_bidding_user_studentcode) %>  : <%= homework.user.student_id%> <% end %> diff --git a/app/views/layouts/base_homework.html.erb b/app/views/layouts/base_homework.html.erb index 81a8b0861..8c108ba22 100644 --- a/app/views/layouts/base_homework.html.erb +++ b/app/views/layouts/base_homework.html.erb @@ -18,8 +18,8 @@ <%= yield :header_tags -%> - <% project = @bid.courses.first %> - <% course = Course.find_by_extra(project.identifier) %> + <% project = @bid.courses.includes(:course_extra).first %> + <% course = project.course_extra %>