# 更改用户名以匹配全名的脚本？

**URL:** <https://meta.discourse.org/t/script-to-change-username-to-match-full-name/115324>\
**Category:** Support\
**Created:** [2019年四月17日 17:48 UTC](https://meta.discourse.org/t/script-to-change-username-to-match-full-name/115324 "2019-04-17T17:48:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidkingham](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/davidkingham/32/119528_2.png) [@davidkingham](https://meta.discourse.org/u/davidkingham)\
**Post date:** [2019年四月17日 17:48 UTC](https://meta.discourse.org/t/script-to-change-username-to-match-full-name/115324/1 "2019-04-17T17:48:59Z")

</div>

On my site we require the use of actual names and no usernames, I have everything set up so only the full name shows, the only issue I have is when a new user is created their username is set to just their first name and not their full name, which looks messy and confusing when @ mentioning someone. I am using memberful as my sso and I know that’s where the problem really lies. I have contacted them and they refuse to change the behavior due to ‘security concerns’ which is absurd, but I’m stuck with it.

So, I’m looking for a way that I could batch rename all users or just new users to match their full name (with an underscore for spaces of course). Any ideas?

---

<div class="post-metadata">

**Author:** ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)\
**Post date:** [2019年四月24日 02:15 UTC](https://meta.discourse.org/t/script-to-change-username-to-match-full-name/115324/2 "2019-04-24T02:15:09Z")

</div>

Something along the lines of:

```plaintext
User.last(10).each do |u|
   u.username = UserNameSuggester.suggest(u.name)
   u.save!
end

```

Careful when iterating to do stuff in batches of say 1000, you will blow memory if you bring in all your users at once.

---

<div class="post-metadata">

**Author:** ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)\
**Post date:** [2022年七月30日 16:23 UTC](https://meta.discourse.org/t/script-to-change-username-to-match-full-name/115324/4 "2022-07-30T16:23:38Z")

</div>


