在我的rails应用中,Exam
belongs_to Questions
,因为考试模型中有一个名为"当前问题"的列。
exam.rb:
belongs_to :current_question, class_name: "Question", foreign_key: "question_id"
当我删除question
时,我希望将current_question设置为已删除问题的任何exams
将current_question设置为nil。
目前,如果我删除一个问题,我会得到这个:
'PG::ForeignKeyViolation: ERROR: update or delete on table "questions" violates foreign key constraint "fk_rails_8b5180f7e7" on table "exams" DETAIL: Key (id)=(1) is still referenced from table "exams". : DELETE FROM "questions" WHERE "questions"."id" = $1'
我打算这样做:a' before_destroy'问题类的回调,它获得与问题相关的所有考试。将current_question设置为nil。
这是最简单的实现吗?