class MigrateShixunStudentWork < ActiveRecord::Migration[5.2]
  def change
    homework_commons = HomeworkCommon.where(homework_type: 4).where("created_at > '2019-07-01 00:00:00'")
    attrs = %i[homework_common_id user_id created_at updated_at]
    StudentWork.bulk_insert(*attrs) do |worker|
      homework_commons.each do |homework|
        course = homework.course
        if course && homework.student_works.count != course.students.count
          course.students.each do |student|
            next if StudentWork.where(homework_common_id: homework.id, user_id: student.user_id).any?
            same_attrs = {user_id: student.user_id}
            worker.add same_attrs.merge(homework_common_id: homework.id)
          end
        end
      end
    end
  end
end
