socialforge/db/migrate/20160511055221_merge_two_ho...

21 lines
740 B
Ruby

class MergeTwoHomeworks < ActiveRecord::Migration
def up
student_works = StudentWork.where("homework_common_id = 3387 and user_id in (11256, 11259, 11252, 11499)")
unless student_works.nil?
student_works.each do |work|
work.update_column('homework_common_id', 3463)
end
end
homework = HomeworkCommon.find 3387
homework.destroy if homework
stu_works = StudentWork.where("homework_common_id = 3387")
stu_work_ids = stu_works.empty? ? "(-1)" : "(" + stu_works.map{|work| work.id}.join(',') + ")"
stu_work_tests = StudentWorkTest.where("student_work_id in #{stu_work_ids}")
stu_work_tests.destroy_all if stu_work_tests
stu_works.destroy_all if stu_works
end
def down
end
end