# 尝试取消星标'Bug Reporter'徽章时出现'FAILED'错误

**URL:** <https://meta.discourse.org/t/failed-error-when-trying-to-un-star-the-bug-reporter-badge/330523>\
**Category:** Bug\
**Tags:** badges, pr-welcome\
**Created:** [2024年十月12日 02:41 UTC](https://meta.discourse.org/t/failed-error-when-trying-to-un-star-the-bug-reporter-badge/330523 "2024-10-12T02:41:44Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![pangbo](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pangbo/32/538562_2.png) [@pangbo](https://meta.discourse.org/u/pangbo)\
**Post date:** [2024年十月13日 07:45 UTC](https://meta.discourse.org/t/failed-error-when-trying-to-un-star-the-bug-reporter-badge/330523/4 "2024-10-13T07:45:56Z")

</div>

这个 bug 已经存在很长时间了。我去年研究过它，以下是重现它的步骤：

1. 作为管理员，将“最多收藏徽章”设置为 2，并创建至少一个可以多次获得的徽章。
2. 作为普通用户，将两个徽章设置为收藏，其中一个是可多次获得的徽章（称为徽章 A）。
3. 作为管理员，再次授予用户徽章 A。
4. 作为普通用户，刷新页面并尝试取消收藏徽章 A。您将遇到错误。

错误的原因是，每次授予可多次获得的徽章时，都会在数据库中创建一个新的 `user_badge` 记录。但是，当用户收藏了该徽章并再次获得它时，新的 `user_badge` 记录不会自动标记为 `is_favorite`。当用户尝试取消收藏徽章 A 时，前端默认会发送最新的 `user_badge` ID。由于此记录未标记为 `is_favorite`，后端会认为用户正在尝试将新徽章设为收藏（而不是取消收藏），这超出了 `max favorite badges` 的限制，从而导致错误。

相关代码位于：

> <https://github.com/discourse/discourse/blob/d3f09f8f61ac33f107fe1503fb4fd928877cfcd4/app/controllers/user_badges_controller.rb#L124-L141>

一个可能的解决方案是将第 131 行修改为：

```ruby
if UserBadge.where(badge: user_badge.badge, user: user_badge.user).pluck(:is_favorite).any? &&

```

然而，这并没有完全解决数据库记录中的不一致问题。

作为临时解决方法，普通用户可以通过在控制台中运行以下 JavaScript 代码来取消收藏所有徽章：

```javascript
const user_name = require("discourse/models/user").default.current().username;
const badges = await require("discourse/models/user-badge").default.findByUsername(user_name);
const favorites = new Map();
badges.filter((b)=>b.is_favorite).forEach((b)=>favorites.set(b.badge_id,b));
favorites.forEach((b)=>b.favorite());

```

---

_[View the full topic](https://meta.discourse.org/t/failed-error-when-trying-to-un-star-the-bug-reporter-badge/330523)._
