diff --git a/db/migrate/20190605021834_migration_exercise_bank_question_number.rb b/db/migrate/20190605021834_migration_exercise_bank_question_number.rb new file mode 100644 index 00000000..fb441763 --- /dev/null +++ b/db/migrate/20190605021834_migration_exercise_bank_question_number.rb @@ -0,0 +1,22 @@ +class MigrationExerciseBankQuestionNumber < ActiveRecord::Migration + def up + Exercise.includes(:exercise_questions).find_each do |exercise| + if exercise.exercise_questions.pluck(:question_number).max != exercise.exercise_questions.size + exercise.exercise_questions.each_with_index do |question, j| + question.update_column('question_number', j + 1) + end + end + end + + ExerciseBank.includes(:exercise_bank_questions).find_each do |exercise| + if exercise.exercise_bank_questions.pluck(:question_number).max != exercise.exercise_bank_questions.size + exercise.exercise_bank_questions.each_with_index do |question, j| + question.update_column('question_number', j + 1) + end + end + end + end + + def down + end +end