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

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

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。例如:

Topic.where(closed: false).where("created_at < '2024-09-24'").where(category_id: cat_id).count
4 个赞