From 00e4170593e53843aeed2f91dfa49fd2aa4fd6d4 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Tue, 28 May 2019 09:46:58 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=BE=E5=A0=82=E7=9A=84school=5Fid=E8=BF=81?= =?UTF-8?q?=E7=A7=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/colleges_controller.rb | 5 ++--- db/migrate/20190528013018_migrate_course_school_id.rb | 11 +++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20190528013018_migrate_course_school_id.rb diff --git a/app/controllers/colleges_controller.rb b/app/controllers/colleges_controller.rb index ad831e78..418015fd 100644 --- a/app/controllers/colleges_controller.rb +++ b/app/controllers/colleges_controller.rb @@ -50,8 +50,7 @@ class CollegesController < ApplicationController # Redo:这样做内存会卡死的 # user_ids = User.find_by_sql("SELECT users.id FROM users LEFT JOIN user_extensions ON users.id=user_extensions.user_id WHERE user_extensions.`school_id` = #{@school.id}").map(&:id) # Redo:是否直接使用count会更好 - all_course_ids = Course.joins("join users u on courses.tea_id = u.id").joins("join user_extensions ue on u.id = ue.user_id"). - where("courses.id != 1309 and courses.is_delete = 0 and ue.school_id = #{@school.id}") + all_course_ids = Course.where("id != 1309 and is_delete = 0 and school_id = #{@school.id}") @courses_count = all_course_ids.size # Redo:对于量比较大的尽量不使用笛卡尔积 @@ -132,7 +131,7 @@ class CollegesController < ApplicationController (select count(m.id) from messages m inner join boards b on b.id=m.board_id and b.parent_id=0 where b.course_id=c.id group by c.id) as messages_count, c.tea_id, c.name, c.is_end, (SELECT MAX(created_at) FROM `course_activities` ca WHERE ca.course_id = c.id) AS update_time - FROM `courses` c join user_extensions ue on ue.user_id = c.tea_id WHERE (ue.school_id = #{@school.id} and c.is_delete = 0)") + FROM `courses` c WHERE c.school_id = #{@school.id} and c.is_delete = 0") @courses.each do |course| course[:evaluating_count] = Output.find_by_sql("select sum(g.evaluate_count) as evaluating_count from games g inner join diff --git a/db/migrate/20190528013018_migrate_course_school_id.rb b/db/migrate/20190528013018_migrate_course_school_id.rb new file mode 100644 index 00000000..b0669591 --- /dev/null +++ b/db/migrate/20190528013018_migrate_course_school_id.rb @@ -0,0 +1,11 @@ +class MigrateCourseSchoolId < ActiveRecord::Migration + def up + courses = Course.includes(teacher: :user_extensions).where("courses.school_id != user_extensions.school_id or courses.school_id is null") + courses.each do |course| + course.update_column('school_id', course.teacher.try(:user_extensions).try(:school_id)) + end + end + + def down + end +end