class MigrateCourseAttendanceMember < ActiveRecord::Migration[5.2]
  def change
    course = Course.find_by(id: 5401)
    if course.present?
      course.course_attendances.each do |attendance|
        course_group_ids = attendance.course_attendance_groups.pluck(:course_group_id)
        students = course_group_ids.include?(0) ? course.students : course.students.where(course_group_id: course_group_ids)
        none_students = students.where.not(user_id: attendance.course_member_attendances.pluck(:user_id))
        none_students.each do |student|
          CourseMemberAttendance.create!(user_id: student.user_id, course_member_id: student.id,
                                         course_group_id: student.course_group_id, course_attendance_id: attendance.id, course_id: course.id)
        end
      end
    end
  end
end
