diff --git a/app/models/gtopic_bank.rb b/app/models/gtopic_bank.rb index af5b267a6..609219711 100644 --- a/app/models/gtopic_bank.rb +++ b/app/models/gtopic_bank.rb @@ -1,7 +1,7 @@ class GtopicBank < ApplicationRecord belongs_to :user - belongs_to :graduation_topic - belongs_to :course_list + belongs_to :graduation_topic, optional: true + belongs_to :course_list, optional: true has_many :attachments, as: :container, dependent: :destroy has_many :graduation_topics, dependent: :nullify diff --git a/db/migrate/20190907021100_migrate_bank_reference_id.rb b/db/migrate/20190907021100_migrate_bank_reference_id.rb new file mode 100644 index 000000000..c71034167 --- /dev/null +++ b/db/migrate/20190907021100_migrate_bank_reference_id.rb @@ -0,0 +1,31 @@ +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