diff --git a/app/models/answer.rb b/app/models/answer.rb index 05f81c6c..5678c566 100644 --- a/app/models/answer.rb +++ b/app/models/answer.rb @@ -17,10 +17,12 @@ class Answer < ActiveRecord::Base before_destroy do # mark a report as deleted if it exists - rep = Report.where(target_id: self.id).first - unless rep.nil? - rep.deleted = true - rep.save + rep = Report.where(target_id: self.id, type: Reports::Answer) + rep.each do |r| + unless r.nil? + r.deleted = true + r.save + end end self.user.decrement! :answered_count diff --git a/app/models/comment.rb b/app/models/comment.rb index ca8f4087..c23070ca 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -14,6 +14,14 @@ class Comment < ActiveRecord::Base end before_destroy do + rep = Report.where(target_id: self.id, type: Reports::Comment) + rep.each do |r| + unless r.nil? + r.deleted = true + r.save + end + end + Subscription.denotify self, answer user.decrement! :commented_count answer.decrement! :comment_count diff --git a/app/models/question.rb b/app/models/question.rb index 27e33b61..1771b6bc 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -6,12 +6,14 @@ class Question < ActiveRecord::Base validates :content, length: { maximum: 255 } before_destroy do - rep = Report.where(target_id: self.id).first - unless rep.nil? - rep.deleted = true - rep.save + rep = Report.where(target_id: self.id, type: Reports::Question) + rep.each do |r| + unless r.nil? + r.deleted = true + r.save + end end - + user.decrement! :asked_count unless self.author_is_anonymous end diff --git a/app/models/user.rb b/app/models/user.rb index d5b0cff1..e5da7c43 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -59,6 +59,25 @@ class User < ActiveRecord::Base end unless website.blank? end + # when a user deleted himself, all reports relating to the user are invalid + before_destroy do + rep = Report.where(target_id: self.id, type: Reports::User) + rep.each do |r| + unless r.nil? + r.deleted = true + r.save + end + end + + rep = Report.where(user_id: self.id) + rep.each do |r| + unless r.nil? + r.deleted = true + r.save + end + end + end + def login=(login) @login = login end