diff --git a/app/models/comment.rb b/app/models/comment.rb index 774ea89c7..a56cc747c 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -7,4 +7,18 @@ class Comment < ApplicationRecord searchable_by :body_plain_text, using: :comments_search_index, as: :body has_markdown :body + + before_destroy :cleanup_events + + private + + def cleanup_events + # Delete events that reference through event_summary + if message&.event_summary.present? + Event.where(summary: message.event_summary).destroy_all + end + + # Delete events that reference directly in particulars + Event.where(particulars: { comment_id: id }).destroy_all + end end