# Algolia插件已损坏：未定义方法\`topic\_count'

**URL:** https://meta.discourse.org/t/algolia-plugin-broken-undefined-method-topic-count/255962
**Category:** Bug
**Tags:** algolia-search
**Created:** [2023年二月22日 17:12 UTC](https://meta.discourse.org/t/algolia-plugin-broken-undefined-method-topic-count/255962 "2023-02-22T17:12:15Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [2023年二月22日 17:12 UTC](https://meta.discourse.org/t/algolia-plugin-broken-undefined-method-topic-count/255962/1 "2023-02-22T17:12:15Z")

</div>

`bundle exec rake algolia:reindex` 因在 [此](https://github.com/discourse/discourse/commit/f122f24b35a8116e47649ae139910d44e3d7488e) commit 后不再存在 `tag.topic_count` 而崩溃。

```plaintext
Clearing users from Algolia
Pushing users to Algolia
..
Successfully pushed 530 users to Algolia
Clearing tags from Algolia
Pushing tags to Algolia
rake aborted!
NoMethodError: undefined method `topic_count' for #<Tag id: 1, name: "installation", created_at: "2021-12-09 11:10:14.145911000 +0000", updated_at: "2021-12-09 11:10:14.145911000 +0000", pm_topic_count: 0, target_tag_id: nil, description: nil, public_topic_count: 3, staff_topic_count: 3>
Did you mean? pm_topic_count
/var/www/discourse/vendor/bundle/ruby/3.1.0/gems/activemodel-7.0.4.1/lib/active_model/attribute_methods.rb:458:in `method_missing'
/var/www/discourse/plugins/discourse-algolia/lib/discourse_algolia/tag_indexer.rb:18:in `should_index?'
/var/www/discourse/plugins/discourse-algolia/lib/discourse_algolia/indexer.rb:22:in `block in process!'
/var/www/discourse/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4.1/lib/active_record/relation/batches.rb:71:in `each'
/var/www/discourse/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4.1/lib/active_record/relation/batches.rb:71:in `block in find_each'
/var/www/discourse/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4.1/lib/active_record/relation/batches.rb:138:in `block in find_in_batches

```

手动将其更改为 `staff_topic_count` 可以解决问题，但可能存在副作用？

```ruby
--- a/lib/discourse_algolia/tag_indexer.rb
+++ b/lib/discourse_algolia/tag_indexer.rb
@@ -15,10 +15,10 @@ class DiscourseAlgolia::TagIndexer < DiscourseAlgolia::Indexer
   end
 
   def should_index?(tag)
- @guardian.can_see?(tag) && tag.topic_count > 0
+ @guardian.can_see?(tag) && tag&.staff_topic_count > 0
   end
 
   def to_object(tag)
- { objectID: tag.id, url: tag.url, name: tag.name, topic_count: tag.topic_count }
+ { objectID: tag.id, url: tag.url, name: tag.name, topic_count: tag.staff_topic_count }
   end
 end

```

---

<div class="post-metadata">

### Author: ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)
#### Post date: [2023年二月23日 23:54 UTC](https://meta.discourse.org/t/algolia-plugin-broken-undefined-method-topic-count/255962/3 "2023-02-23T23:54:36Z")

</div>

感谢您举报 @RGJ

我已经修复了这个问题

> <https://github.com/discourse/discourse-algolia/pull/82>
>
> Discourse core commit f122f24b35a8116e47649ae139910d44e3d7488e removed
> \`Tag#topi…c\_count\` as part of a security fix. This commit updates the
> plugin to rely on \`Tag.topic\_count\_column\` instead which ensures that
> the tag topic count being indexed by default does not include read
> restricted topic.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2023年二月24日 04:22 UTC](https://meta.discourse.org/t/algolia-plugin-broken-undefined-method-topic-count/255962/4 "2023-02-24T04:22:08Z")

</div>


