# 是否有用于设置用户的主要组的API

**URL:** <https://meta.discourse.org/t/is-there-an-api-to-set-the-primary-group-for-a-user/309611>\
**Category:** Development\
**Tags:** rest-api\
**Created:** [2024 年5 月 28 日 00:29 UTC](https://meta.discourse.org/t/is-there-an-api-to-set-the-primary-group-for-a-user/309611 "2024-05-28T00:29:24Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

作者： ![Onlyanegg](https://avatars.discourse-cdn.com/v4/letter/o/e8c25b/32.png) [@Onlyanegg](https://meta.discourse.org/u/Onlyanegg)\
发布日期： [2024 年5 月 28 日 04:34 UTC](https://meta.discourse.org/t/is-there-an-api-to-set-the-primary-group-for-a-user/309611/3 "2024-05-28T04:34:39Z")

</div>

感谢您的提示——我尝试使用了它——我更新了 `primary_group_name`、`primary_group_id`，甚至还更新了组数组条目 `['primary_group'] = true`——当我调用 `update_user` 时，我得到了一个 200 回复——使用了 gem，但还没有成功。我明天会仔细看看，看看是否有任何迹象表明出了什么问题。但我没有发现任何明显的问题。

这是我运行并从服务器获得 200 代码的代码：

```plaintext
client = DiscourseApi::Client.new("https://myforumurl.example.com")
client.api_key = '用于测试此功能的具有全局权限的 api 密钥'
client.api_username = '我的管理员用户名——我认为这无关紧要'
user = client.user('joepowell')
user['primary_group_name'] = 'dojo'
user['primary_group_id'] = 42
# 这是组数组中的组 42
user['groups'][2]['primary_group'] = true
client.update_user('joepowell', user)

```

---

_[View the full topic](https://meta.discourse.org/t/is-there-an-api-to-set-the-primary-group-for-a-user/309611)._
