课堂的school_id迁移

pre_develop
cxt 6 years ago
parent f14bf75b30
commit 00e4170593

@ -50,8 +50,7 @@ class CollegesController < ApplicationController
# Redo这样做内存会卡死的 # 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) # 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会更好 # 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"). all_course_ids = Course.where("id != 1309 and is_delete = 0 and school_id = #{@school.id}")
where("courses.id != 1309 and courses.is_delete = 0 and ue.school_id = #{@school.id}")
@courses_count = all_course_ids.size @courses_count = all_course_ids.size
# Redo对于量比较大的尽量不使用笛卡尔积 # 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, (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, 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 (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| @courses.each do |course|
course[:evaluating_count] = Output.find_by_sql("select sum(g.evaluate_count) as evaluating_count from games g inner join course[:evaluating_count] = Output.find_by_sql("select sum(g.evaluate_count) as evaluating_count from games g inner join

@ -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
Loading…
Cancel
Save