# 大量关闭比x更旧的现有主题？

**URL:** <https://meta.discourse.org/t/mass-close-existing-topics-older-than-x/328214>\
**Category:** Support\
**Created:** [2024年九月26日 12:00 UTC](https://meta.discourse.org/t/mass-close-existing-topics-older-than-x/328214 "2024-09-26T12:00:42Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)\
**Post date:** [2024年九月26日 17:39 UTC](https://meta.discourse.org/t/mass-close-existing-topics-older-than-x/328214/5 "2024-09-26T17:39:31Z")

</div>

> [@Tealk](#):
>
> 如何将其缩小到一个类别？

您可以找到类别 ID，然后将其作为命令中的附加条件。例如，关闭“general”类别中早于 9 月 24 日创建的所有未关闭的主题：

```ruby
cat_id = Category.find_by_slug('general').id
Topic.where(closed: false).where("created_at < '2024-09-24'").where(category_id: cat_id).find_each do |topic|
  topic.update_status('closed', true, Discourse.system_user)
end

```

这类操作总是让我有点紧张。请务必在运行它们之前创建数据库备份，以防万一出现问题。运行某种初步检查以确保您正在操作正确的数据也可能是一个好主意。一种方法是返回操作将执行的主题的 `count`。例如：

```ruby
Topic.where(closed: false).where("created_at < '2024-09-24'").where(category_id: cat_id).count

```

---

_[View the full topic](https://meta.discourse.org/t/mass-close-existing-topics-older-than-x/328214)._
