class DeleteExcessStudentWorks < ActiveRecord::Migration def up homework = HomeworkCommon.where(:id => 4818) unless homework.empty? homework = homework.first homework.course.student.each do |student| student_works = homework.student_works.where(:user_id => student.student_id).reorder("created_at asc") if student_works.count > 1 if student_works.first.work_status != 0 student_works.last.destroy elsif student_works.last.work_status != 0 student_works.first.destroy else student_works.first.destroy end end end end end def down end end