diff --git a/app/models/exercise.rb b/app/models/exercise.rb index af84a5c49..60409e51d 100644 --- a/app/models/exercise.rb +++ b/app/models/exercise.rb @@ -3,11 +3,11 @@ class Exercise < ApplicationRecord belongs_to :exercise_bank, optional: true belongs_to :user - has_many :exercise_users - has_many :exercise_questions - has_many :exercise_group_settings + has_many :exercise_users, :dependent => :delete_all + has_many :exercise_questions, :dependent => :delete_all + has_many :exercise_group_settings, :dependent => :delete_all has_many :tidings, as: :container - has_many :course_acts, class_name: 'CourseActivity', as: :course_act, dependent: :destroy + has_many :course_acts, class_name: 'CourseActivity', as: :course_act, :dependent => :delete_all scope :is_exercise_published, -> { where("exercise_status > ? ",1)} scope :unified_setting, -> { where("unified_setting = ?",true) } @@ -19,13 +19,6 @@ 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 96fbe17bb..11558f937 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 + has_many :exercise_answer_comments, :dependent => :delete_all scope :search_exercise_answer, lambda { |name,key| where("#{name} = ?",key)} scope :search_answer_users, lambda {|name,ids| where("#{name}":ids)} @@ -11,10 +11,4 @@ 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 f4f5c5a16..72dc30c9d 100644 --- a/app/models/exercise_choice.rb +++ b/app/models/exercise_choice.rb @@ -1,16 +1,10 @@ class ExerciseChoice < ApplicationRecord belongs_to :exercise_question - has_many :exercise_answers - has_many :exercise_standard_answers + has_many :exercise_answers, :dependent => :delete_all + has_many :exercise_standard_answers, :dependent => :delete_all 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 c236b657c..2c9b281c3 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 + has_many :exercise_choices, :dependent => :delete_all has_many :exercise_answers - has_many :exercise_shixun_challenges + has_many :exercise_shixun_challenges, :dependent => :delete_all has_many :exercise_shixun_answers has_many :exercise_answer_comments has_many :exercise_standard_answers @@ -15,14 +15,7 @@ class ExerciseQuestion < ApplicationRecord scope :find_by_custom, lambda {|k,v| where("#{k} = ?",v)} #根据传入的参数查找问题 scope :left_question_choose, lambda {|k,v| where("#{k} > ?",v)} #根据传入的参数查找问题 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 72a6999d6..a9550c024 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 + has_many :exercise_answer_comments, :dependent => :delete_all # status 0: 未通过, 1:通过 # attr_accessible :answer_text, :score, :status scope :search_shixun_answers, lambda {|name,ids| where("#{name}":ids)} @@ -10,9 +10,4 @@ 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