任何在控制台执行的破坏性操作都难免让人有些紧张。运行之前,请务必先做好备份。
CategoryUser 记录描述了用户对某个类别的通知级别。notification_level 为 0 表示该类别对该用户处于静音状态。你可以在此了解更多相关信息:https://meta.discourse.org/t/how-do-i-set-category-tracking-level-defaults-historically/53165。
删除所有 category_id 为指定值且 notification_level 为 0 的 CategoryUser 记录,将会从你网站上所有用户的静音列表中移除该类别。
当我运行此类命令时,通常还会额外增加一个步骤(上文未列出)。我不会直接运行:
CategoryUser.where(category_id: c.id, notification_level: 0).destroy_all
而是先将记录赋值给一个变量,以便确认处理的是正确的数据。类似这样:
# 获取类别并赋值给变量
c = Category.find_by(name: "<你的类别名称>")
# 将 category_users 赋值给变量并检查数据。确保记录具有正确的 category_id 等。
muted_category_users = CategoryUser.where(category_id: c.id, notification_level: 0)
# 确认数据无误后,再运行
muted_category_users.destroy_all