A platform for community discussion. Free, open, simple.
FIX: error when deleting a tag associated with a deleted topic
Neil Lalonde81e873138fa2
diff --git a/app/models/topic_tag.rb b/app/models/topic_tag.rbindex f0996d1c9d7..067e1f79bf1 100644--- a/app/models/topic_tag.rb+++ b/app/models/topic_tag.rb@@ -3,7 +3,7 @@ class TopicTag < ActiveRecord::Basebelongs_to :tag, counter_cache: "topic_count"after_create do- if topic.category_id+ if topic&.category_idif stat = CategoryTagStat.where(tag_id: tag_id, category_id: topic.category_id).firststat.increment!(:topic_count)else@@ -13,7 +13,7 @@ class TopicTag < ActiveRecord::Baseendafter_destroy do- if topic.category_id+ if topic&.category_idif stat = CategoryTagStat.where(tag_id: tag_id, category: topic.category_id).firststat.topic_count == 1 ? stat.destroy : stat.decrement!(:topic_count)enddiff --git a/spec/models/tag_spec.rb b/spec/models/tag_spec.rbindex 6cea43f3052..06287133a5d 100644--- a/spec/models/tag_spec.rb+++ b/spec/models/tag_spec.rb@@ -15,6 +15,13 @@ describe Tag doSiteSetting.min_trust_level_to_tag_topics = 0end+ it "can delete tags on deleted topics" do+ tag = Fabricate(:tag)+ topic = Fabricate(:topic, tags: [tag])+ topic.trash!+ expect { tag.destroy }.to change { Tag.count }.by(-1)+ end+describe '#tags_by_count_query' doit "returns empty hash if nothing is tagged" doexpect(described_class.tags_by_count_query.count(Tag::COUNT_ARG)).to eq({})