class ChangeExercise1930Position < ActiveRecord::Migration[5.2]
  def change
    exs = Exercise.all.includes(:exercise_questions)
    exs.each do |ex|
      ex_questions = ex&.exercise_questions&.select(:id,:question_number,:exercise_id)&.order("question_number ASC")  #试卷的位置迁移
      if ex_questions.present?
        ex_questions.each_with_index do |q,index|
          puts index
          q_num = index + 1
          if q.question_number.to_i != q_num
            q.update_attributes(question_number: q_num)
          end
        end
      end

      ex_q_bank = ex&.exercise_bank&.exercise_bank_questions&.select(:id,:question_number,:exercise_bank_id,:shixun_id)&.order("question_number ASC") #试卷的习题库位置迁移
      if ex_q_bank.present?
        ex_q_bank.each_with_index do |q,index|
          puts index
          q_num_1 = index + 1
          if q.question_number.to_i != q_num_1
            q.update_attributes(question_number: q_num_1)
          end
        end
      end
    end

  end
end
