From 016d59bcaad629cbc982e03e4b5922d73e1e41fc Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Thu, 19 Mar 2020 16:28:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E8=AF=BE=E5=A0=82=E7=9A=84?= =?UTF-8?q?=E5=AD=A6=E7=94=9F=E4=BD=9C=E5=93=81=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tasks/migrate_course_student_work.rake | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 lib/tasks/migrate_course_student_work.rake diff --git a/lib/tasks/migrate_course_student_work.rake b/lib/tasks/migrate_course_student_work.rake new file mode 100644 index 000000000..c795a49ce --- /dev/null +++ b/lib/tasks/migrate_course_student_work.rake @@ -0,0 +1,31 @@ +# 执行示例 bundle exec rake migrate_course_student_work:homework args=2933 +desc "创建课堂学生的作业数据" + +namespace :migrate_course_student_work do + if ENV['args'] + course_id = ENV['args'].split(",")[0] # 对应课堂的id + end + + task homework: :environment do + course = Course.find_by(id: course_id) + if course.present? + student_ids = course.students.pluck(:user_id) + + # 如果之前存在相关作品,则更新is_delete字段 + student_works = StudentWork.joins(:homework_common).where(user_id: student_ids, homework_commons: {course_id: course.id}) + student_works.update_all(is_delete: 0) + + attrs = %i[homework_common_id user_id created_at updated_at] + + StudentWork.bulk_insert(*attrs) do |worker| + student_ids.each do |user_id| + same_attrs = {user_id: user_id} + course.homework_commons.where(homework_type: %i[normal group practice]).each do |homework| + next if StudentWork.where(user_id: user_id, homework_common_id: homework.id).any? + worker.add same_attrs.merge(homework_common_id: homework.id) + end + end + end + end + end +end