class MigrateBankReferenceId < ActiveRecord::Migration[5.2] def change HomeworkBank.all.each do |bank| if bank.homework_common bank.homework_common.update_column("homework_bank_id", bank.id) if bank.homework_common.homework_bank_id.nil? end end GtopicBank.all.each do |bank| if bank.graduation_topic bank.graduation_topic.update_column("gtopic_bank_id", bank.id) if bank.graduation_topic.gtopic_bank_id.nil? end end GtaskBank.all.each do |bank| if bank.graduation_task bank.graduation_task.update_column("gtask_bank_id", bank.id) if bank.graduation_task.gtask_bank_id.nil? end end ExerciseBank.all.each do |bank| if bank.container_type == 'Exercise' exercise = Exercise.find_by(id: bank.container_id) exercise.update_column("exercise_bank_id", bank.id) if exercise && exercise.exercise_bank_id.nil? elsif bank.container_type == 'Poll' poll = Poll.find_by(id: bank.container_id) poll.update_column("exercise_bank_id", bank.id) if poll && poll.exercise_bank_id.nil? end end end end