FIX: Delete related search data when record has been deleted.

FIX: Delete related search data when record has been deleted.

diff --git a/app/models/category.rb b/app/models/category.rb
index cb51a03..cfd9de5 100644
--- a/app/models/category.rb
+++ b/app/models/category.rb
@@ -47,6 +47,8 @@ class Category < ActiveRecord::Base
 
   has_and_belongs_to_many :web_hooks
 
+  has_one :category_search_data, dependent: :delete
+
   validates :user_id, presence: true
 
   validates :name, if: Proc.new { |c| c.new_record? || c.will_save_change_to_name? },
diff --git a/app/models/post.rb b/app/models/post.rb
index a9880a8..de9bb44 100644
--- a/app/models/post.rb
+++ b/app/models/post.rb
@@ -52,6 +52,7 @@ class Post < ActiveRecord::Base
   has_many :revisions, -> { order(:number) }, foreign_key: :post_id, class_name: 'PostRevision'
 
   has_many :user_actions, foreign_key: :target_post_id
+  has_one :post_search_data, dependent: :delete
 
   belongs_to :image_upload, class_name: "Upload"
 
diff --git a/app/models/tag.rb b/app/models/tag.rb
index 6ff6afe..7b18643 100644
--- a/app/models/tag.rb
+++ b/app/models/tag.rb
@@ -39,6 +39,8 @@ class Tag < ActiveRecord::Base
   belongs_to :target_tag, class_name: "Tag", optional: true
   has_many :synonyms, class_name: "Tag", foreign_key: "target_tag_id", dependent: :destroy
 
+  has_one :tag_search_data, dependent: :delete
+
   after_save :index_search
   after_save :update_synonym_associations
 
diff --git a/app/models/topic.rb b/app/models/topic.rb
index a269988..a32c07d 100644
--- a/app/models/topic.rb
+++ b/app/models/topic.rb
@@ -224,7 +224,7 @@ class Topic < ActiveRecord::Base
 
   has_one :user_warning
   has_one :first_post, -> { where post_number: 1 }, class_name: 'Post'
-  has_one :topic_search_data
+  has_one :topic_search_data, dependent: :delete
   has_one :topic_embed, dependent: :destroy
 
   belongs_to :image_upload, class_name: 'Upload'
diff --git a/app/models/user.rb b/app/models/user.rb
index acbcdb0..66916d6 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -48,6 +48,7 @@ class User < ActiveRecord::Base
   has_one :anonymous_user_master, class_name: 'AnonymousUser', dependent: :destroy
   has_one :anonymous_user_shadow, ->(record) { where(active: true) }, foreign_key: :master_user_id, class_name: 'AnonymousUser', dependent: :destroy
   has_one :invited_user, dependent: :destroy
+  has_one :user_search_data, dependent: :delete
 
   # delete all is faster but bypasses callbacks
   has_many :bookmarks, dependent: :delete_all

GitHub sha: ecc799ab

1 Like

Should we have a test for that?

1 Like

bump @tgxworld - I don’t like things to sit in review for too long.

Actually this commit didn’t solve anything because we’ve always been cleaning up in discourse/searchable.rb at cbe1dd8ec73197f69cd113fdf3fd87096951cfa1 · discourse/discourse · GitHub

1 Like