From 2a010194578c773162e856a6812ea74adb83b667 Mon Sep 17 00:00:00 2001 From: SylorHuang Date: Fri, 28 Jun 2019 12:23:10 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=95=E5=8D=B7=E5=88=A0=E9=99=A4=E7=9A=84?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/exercise.rb | 13 ++++++++++--- app/models/exercise_answer.rb | 8 +++++++- app/models/exercise_choice.rb | 11 +++++++++-- app/models/exercise_question.rb | 10 ++++++++-- app/models/exercise_shixun_answer.rb | 7 ++++++- 5 files changed, 40 insertions(+), 9 deletions(-) diff --git a/app/models/exercise.rb b/app/models/exercise.rb index 0c8b97ca7..af84a5c49 100644 --- a/app/models/exercise.rb +++ b/app/models/exercise.rb @@ -3,9 +3,9 @@ class Exercise < ApplicationRecord belongs_to :exercise_bank, optional: true belongs_to :user - has_many :exercise_users,:dependent => :destroy - has_many :exercise_questions,:dependent => :destroy - has_many :exercise_group_settings,:dependent => :destroy + has_many :exercise_users + has_many :exercise_questions + has_many :exercise_group_settings has_many :tidings, as: :container has_many :course_acts, class_name: 'CourseActivity', as: :course_act, dependent: :destroy @@ -19,6 +19,13 @@ class Exercise < ApplicationRecord validates :exercise_name, length: { maximum: 60, too_long: "60 characters is the maximum allowed" } after_create :create_exercise_list + after_destroy :destroy_other_relations + + def destroy_other_relations + exercise_users.destroy_all + exercise_questions.destroy_all + exercise_group_settings.destroy_all + end def create_exercise_list str = "" diff --git a/app/models/exercise_answer.rb b/app/models/exercise_answer.rb index 465fa036b..96fbe17bb 100644 --- a/app/models/exercise_answer.rb +++ b/app/models/exercise_answer.rb @@ -3,7 +3,7 @@ class ExerciseAnswer < ApplicationRecord belongs_to :user belongs_to :exercise_question belongs_to :exercise_choice, optional: true - has_many :exercise_answer_comments, :dependent => :destroy + has_many :exercise_answer_comments scope :search_exercise_answer, lambda { |name,key| where("#{name} = ?",key)} scope :search_answer_users, lambda {|name,ids| where("#{name}":ids)} @@ -11,4 +11,10 @@ class ExerciseAnswer < ApplicationRecord scope :exercise_answer_is_right, -> {where("score > ?",0.0)} #判断答案是否正确,根据分数总和大于0 scope :score_reviewed, lambda {where("score >= ?",0.0)} #是否评分,用于判断主观题的 + after_destroy :destroy_answers_relations + + def destroy_answers_relations + exercise_answer_comments.destroy_all + end + end \ No newline at end of file diff --git a/app/models/exercise_choice.rb b/app/models/exercise_choice.rb index 525251ce6..f4f5c5a16 100644 --- a/app/models/exercise_choice.rb +++ b/app/models/exercise_choice.rb @@ -1,9 +1,16 @@ class ExerciseChoice < ApplicationRecord belongs_to :exercise_question - has_many :exercise_answers, :dependent => :destroy - has_many :exercise_standard_answers, :dependent => :destroy + has_many :exercise_answers + has_many :exercise_standard_answers scope :find_choice_custom, lambda {|k,v| where("#{k} = ?",v)} #根据传入的参数查找问题 scope :left_choice_choose, lambda {|k,v| where("#{k} > ?",v)} #根据传入的参数查找问题 + + after_destroy :destroy_choices_relations + + def destroy_choices_relations + exercise_answers.destroy_all + exercise_standard_answers.destroy_all + end end \ No newline at end of file diff --git a/app/models/exercise_question.rb b/app/models/exercise_question.rb index 607ad1a29..c236b657c 100644 --- a/app/models/exercise_question.rb +++ b/app/models/exercise_question.rb @@ -4,9 +4,9 @@ class ExerciseQuestion < ApplicationRecord belongs_to :exercise belongs_to :shixun, optional: true - has_many :exercise_choices, :dependent => :destroy + has_many :exercise_choices has_many :exercise_answers - has_many :exercise_shixun_challenges,:dependent => :destroy + has_many :exercise_shixun_challenges has_many :exercise_shixun_answers has_many :exercise_answer_comments has_many :exercise_standard_answers @@ -17,6 +17,12 @@ class ExerciseQuestion < ApplicationRecord scope :find_objective_questions, -> {where("question_type != ?",4)} #查找全部客观题 # scope :next_exercise, lambda {|k| where("question_number > ?",k).first} # scope :last_exercise, lambda {|k| where("question_number < ?",k).last} + after_destroy :destroy_questions_relations + + def destroy_questions_relations + exercise_choices.destroy_all + exercise_shixun_challenges.destroy_all + end def question_type_name case self.question_type diff --git a/app/models/exercise_shixun_answer.rb b/app/models/exercise_shixun_answer.rb index 8548e497d..72a6999d6 100644 --- a/app/models/exercise_shixun_answer.rb +++ b/app/models/exercise_shixun_answer.rb @@ -2,7 +2,7 @@ class ExerciseShixunAnswer < ApplicationRecord belongs_to :exercise_question belongs_to :user belongs_to :exercise_shixun_challenge - has_many :exercise_answer_comments, :dependent => :destroy + has_many :exercise_answer_comments # status 0: 未通过, 1:通过 # attr_accessible :answer_text, :score, :status scope :search_shixun_answers, lambda {|name,ids| where("#{name}":ids)} @@ -10,4 +10,9 @@ class ExerciseShixunAnswer < ApplicationRecord scope :shixun_no_full_scores, lambda { |score| where("score > 0.0 AND score < ?",score)} scope :score_reviewed, lambda {where("score is not NULL AND score >= ?",0.0)} #是否评分 + after_destroy :destroy_shixun_relations + + def destroy_shixun_relations + exercise_answer_comments.destroy_all + end end