diff --git a/app/models/post.rb b/app/models/post.rb index d401bdf71..34a929e4c 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -400,24 +400,16 @@ class Post < ApplicationRecord set_tag_count(category, self.send("tag_count_#{category}") + 1) end - def set_tag_counts(disable_cache = true) + def set_tag_counts self.tag_count = 0 TagCategory.categories.each {|x| set_tag_count(x, 0)} - categories = Tag.categories_for(tag_array, :disable_caching => disable_cache) + categories = Tag.categories_for(tag_array, disable_caching: true) categories.each_value do |category| self.tag_count += 1 inc_tag_count(TagCategory.reverse_mapping[category]) end end - def fix_post_counts(post) - post.set_tag_counts(false) - if post.changes_saved? - args = Hash[TagCategory.categories.map {|x| ["tag_count_#{x}", post.send("tag_count_#{x}")]}].update(:tag_count => post.tag_count) - post.update_columns(args) - end - end - def merge_old_changes reset_tag_array_cache @removed_tags = [] diff --git a/app/models/tag.rb b/app/models/tag.rb index 2dc756445..731f2aee7 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -10,8 +10,8 @@ class Tag < ApplicationRecord validates :name, tag_name: true, on: :name validates_inclusion_of :category, in: TagCategory.category_ids - before_save :update_category_cache, if: :category_changed? - before_save :update_category_post_counts, if: :category_changed? + after_save :update_category_cache, if: :saved_change_to_category? + after_save :update_category_post_counts, if: :saved_change_to_category? scope :empty, -> { where("tags.post_count <= 0") } scope :nonempty, -> { where("tags.post_count > 0") } @@ -162,12 +162,10 @@ class Tag < ApplicationRecord end def update_category_post_counts - Post.with_timeout(30_000, nil, :tags => name) do - Post.raw_tag_match(name).where("true /* Tag#update_category_post_counts */").find_each do |post| - post.reload - post.set_tag_counts(false) - args = TagCategory.categories.map {|x| ["tag_count_#{x}", post.send("tag_count_#{x}")]}.to_h.update(:tag_count => post.tag_count) - Post.where(:id => post.id).update_all(args) + Post.with_timeout(30_000) do + Post.raw_tag_match(name).find_each do |post| + post.set_tag_counts + post.save! end end end