# 当主题存在其他四个标签中的任何一个时，需要添加标签

**URL:** <https://meta.discourse.org/t/need-to-add-tag-when-any-of-4-other-tags-exist-on-topic/226421>\
**Category:** Support\
**Created:** [2022年五月7日 17:02 UTC](https://meta.discourse.org/t/need-to-add-tag-when-any-of-4-other-tags-exist-on-topic/226421 "2022-05-07T17:02:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fma965](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fma965/32/259795_2.png) [@Fma965](https://meta.discourse.org/u/Fma965)\
**Post date:** [2022年五月7日 17:02 UTC](https://meta.discourse.org/t/need-to-add-tag-when-any-of-4-other-tags-exist-on-topic/226421/1 "2022-05-07T17:02:08Z")

</div>

大家好，

我最近对我的论坛进行了一些重构，并开始使用标签。

我正在努力解决的一个问题是（我知道这不是原生功能，但也许有简单的插件？）当创建一个带有标签“sync1”、“sync2”、“sync3”或“sync4”的新主题时，我还需要附加标签“apim”。有人知道我该如何做到这一点吗？

不幸的是，我的 Ruby 技能有所欠缺。  
我看到过这两个链接：

> <https://github.com/discourse/discourse-unhandled-tagger/blob/main/plugin.rb>

和

> [@Topic auto tagging](https://meta.discourse.org/t/topic-auto-tagging/54894/18):
>
> You can create a plugin with the below simple code. But the difficulty level is fully depends upon your auto tagging logic/requirement. DiscourseEvent.on(:topic\_created) do |topic| guardian = Guardian.new(Discourse.system\_user) DiscourseTagging.tag\_topic\_by\_names(topic, guardian, ["bug"], append: true) if topic.title.match(/bug/) end

但我不知道如何将其改编为仅检查主题上是否存在这 4 个标签中的任何一个，而不是像这些示例那样进行检查。

基本上，如果粉色标签中的任何一个存在于主题中，就应该自动添加绿色标签。

 ![image](https://global.discourse-cdn.com/meta/original/4X/0/2/2/0224644b66575e1cf745d93fc5b63596942a8ee5.png)

---

<div class="post-metadata">

**Author:** ![vinothkannans](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vinothkannans/32/86465_2.png) [@vinothkannans](https://meta.discourse.org/u/vinothkannans)\
**Post date:** [2022年五月7日 18:09 UTC](https://meta.discourse.org/t/need-to-add-tag-when-any-of-4-other-tags-exist-on-topic/226421/2 "2022-05-07T18:09:31Z")

</div>

```plaintext
DiscourseEvent.on(:topic_created) do |topic|
  guardian = Guardian.new(Discourse.system_user)
  required_tag_names = [”sync1”, “sync2”, “sync3”, “sync4”]
  DiscourseTagging.tag_topic_by_names(topic, guardian, [\"apim\"], append: true) if topic.tags.exists?(name: required_tag_names)
end

```

你需要这个 ⬆ 代码。

编辑：你也可以通过添加一个标签组并将“apim”设置为父标签来强制执行。在这种情况下，用户只能在选择父标签“apim”后选择“sync”标签。

* * *

我认为如果我们允许用户先选择子标签，然后自动添加父标签，那会更有用 🤷 大多数最终用户不会知道父标签的要求。

---

<div class="post-metadata">

**Author:** ![Fma965](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fma965/32/259795_2.png) [@Fma965](https://meta.discourse.org/u/Fma965)\
**Post date:** [2022年五月7日 18:46 UTC](https://meta.discourse.org/t/need-to-add-tag-when-any-of-4-other-tags-exist-on-topic/226421/3 "2022-05-07T18:46:41Z")

</div>

太好了，我通常会自己解决这些问题，但 Ruby 不是我的专长，我感觉会有人在几分钟内就给出一个例子 🙂

现在我只需要弄清楚如何将它添加到插件中，但我确定这很简单。

我同意，有时选择一个会添加父级的子标签可能是首选结果。

感谢您的帮助。

编辑：插件已创建并按预期工作，非常感谢您的帮助！

> **[GitHub - cyanlabs/discourse-append-tag-apim: Simple plugin that appends the apim tag to...](https://github.com/CyanLabs/discourse-append-tag-apim)**
>
> Simple plugin that appends the apim tag to sync1,sync2,sync3,sync4 tagged topics

---

<div class="post-metadata">

**Author:** ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)\
**Post date:** [2022年六月6日 18:46 UTC](https://meta.discourse.org/t/need-to-add-tag-when-any-of-4-other-tags-exist-on-topic/226421/4 "2022-06-06T18:46:48Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
