From 8f70ad805389ef47c4c40ee59d7e5363a7e2cce1 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Wed, 5 Jun 2019 10:54:10 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A2=98=E5=BA=8F=E8=BF=81=E7=A7=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...migration_exercise_bank_question_number.rb | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 db/migrate/20190605021834_migration_exercise_bank_question_number.rb 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