From aba01c8239c46dfc8c4ecd8bf3f69ed4b5cf355e Mon Sep 17 00:00:00 2001 From: Bohung Date: Thu, 20 Jan 2022 15:57:13 +0800 Subject: [PATCH] Fix bug. --- app/models/seminar_main.rb | 3 ++- app/models/seminar_review.rb | 41 +++++++++++++++++++++--------------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/app/models/seminar_main.rb b/app/models/seminar_main.rb index ce56eba..fd41361 100644 --- a/app/models/seminar_main.rb +++ b/app/models/seminar_main.rb @@ -176,7 +176,8 @@ class SeminarMain end end after_initialize do - unless self.new_record? + unless self.new_record? || @triggered_initialize + @triggered_initialize = true save_flag = false seminar_reviews = self.seminar_reviews if seminar_reviews.present? diff --git a/app/models/seminar_review.rb b/app/models/seminar_review.rb index 14d52e9..8331d77 100644 --- a/app/models/seminar_review.rb +++ b/app/models/seminar_review.rb @@ -20,31 +20,38 @@ class SeminarReview field :seminar_session_ids belongs_to :seminar_main after_initialize do - unless self.new_record? + unless self.new_record? || @triggered_initialize + @triggered_initialize = true save_flag = false + destroy_flag = false if (self.reviewer_id.class == Array rescue false) reviewer_ids = self.reviewer_id[1..-1] reviewer_ids.each do |reviewer_id| - self.class.create(:reviewer_id=>reviewer_id,:seminar_main=>self.seminar_main) + self.class.create(:reviewer_id=>reviewer_id,:seminar_main_id=>self.seminar_main_id) end self.reviewer_id = self.reviewer_id.first save_flag = true + elsif self[:reviewer_id].nil? + self.destroy + destroy_flag = true end - if (self.session_ids.nil? rescue true) - session_ids = [] - self.default_topics.each_with_index do |t,i| - next if t != "seminar_signup_contributes.description" - session_ids << self.default_topics_values[i] + if !destroy_flag + if (self.session_ids.nil? rescue true) + session_ids = [] + self.default_topics.each_with_index do |t,i| + next if t != "seminar_signup_contributes.description" + session_ids << self.default_topics_values[i] + end + session_ids = session_ids.flatten.map{|i| i.to_i} + self.session_ids = session_ids + save_flag = true end - session_ids = session_ids.flatten.map{|i| i.to_i} - self.session_ids = session_ids - save_flag = true + if self.seminar_session_ids.nil? && self.session_ids.class == Array + self.seminar_session_ids = SeminarSession.where(:seminar_main_id=>self.seminar_main_id,:key.in=>self.session_ids).pluck(:id) rescue nil + save_flag = true + end + self.save(:validate=>false) if save_flag end - if self.seminar_session_ids.nil? && self.session_ids.class == Array - self.seminar_session_ids = self.seminar_main.seminar_sessions.where(:key.in=>self.session_ids).pluck(:id) rescue nil - save_flag = true - end - self.save(:validate=>false) if save_flag end end before_save do @@ -60,12 +67,12 @@ class SeminarReview default_seminar_signup_ids += SeminarSignupContribute.where(:id.in=>seminar_signup_contribute_ids).pluck(:seminar_signup_id) elsif fn == "seminar_signup_contributes" seminar_signup_ids_from_contribute = SeminarSignupContribute.where(:description.in=>self.default_topics_values[i].to_a).pluck(:seminar_signup_id) - seminar_signup_ids_from_contribute = seminar_signup_ids_from_contribute & (self.seminar_main.seminar_signups.pluck(:id) rescue []) + seminar_signup_ids_from_contribute = seminar_signup_ids_from_contribute & (SeminarSignup.where(:seminar_main_id=>self.seminar_main_id).pluck(:id) rescue []) default_seminar_signup_ids += seminar_signup_ids_from_contribute self.session_ids = self.session_ids.concat(self.default_topics_values[i].map{|v| v.to_i}) end end - self.seminar_session_ids = self.seminar_main.seminar_sessions.where(:key.in=>self.session_ids.to_a).pluck(:id) rescue [] + self.seminar_session_ids = SeminarSession.where(:seminar_main_id=>self.seminar_main_id,:key.in=>self.session_ids.to_a).pluck(:id) rescue [] default_seminar_signup_ids.uniq! default_seminar_signup_ids = default_seminar_signup_ids.map{|s| s.to_s} self.default_seminar_signup_ids = default_seminar_signup_ids