# Badge Granting in bulk or for a group of users

**URL:** https://meta.discourse.org/t/badge-granting-in-bulk-or-for-a-group-of-users/36782
**Category:** Feature
**Created:** [2015 年 12 月 16 日午後 1:54 UTC](https://meta.discourse.org/t/badge-granting-in-bulk-or-for-a-group-of-users/36782 "2015-12-16T13:54:29Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![pacharanero](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pacharanero/32/500583_2.png) [@pacharanero](https://meta.discourse.org/u/pacharanero)
#### Post date: [2015 年 12 月 16 日午後 1:54 UTC](https://meta.discourse.org/t/badge-granting-in-bulk-or-for-a-group-of-users/36782/1 "2015-12-16T13:54:29Z")

</div>

So far as I can see there is no facility for granting Badges in bulk except for via the SQL query in the Badge Edit / New Badge page at /admin/badges/

In my Discourse instances we award Badges to people for accolades or for attendance at certain events.

Quite often I find myself wanting to grant badges to a number of users. The group is otherwise arbitrary, ie there is nothing in Discourse’s database that could be used to decide if they should get the badge or not, so a SQL query can’t be used.

I can appreciate this is going to be a low priority feature request (as I reckon it’s maybe a bit of an edge case, and I’m not one of Discourse’s paying customers 🙂 ) but I thought I’d ask anyway.

A ‘grant badges’ UI which would let me add a number of users via CSV file would be awesome - something a bit like the Bulk Invite functionality would do the job.

For the time being, and to help anyone else who has the same problem, I currently grant bulk badges in the Rails console using this code:

```plaintext
# start with a list of emails as an array
# I prepare the list from a CSV list I am given of event attendees
# then I use Sublime Text to get it right, before pasting into the rails console)
emails = ["user1@example.com", "user2@example.com", "user3@example.com" ..... etc]

# initialize blank array for User objects you're going to collect
userlist = [] 

# find emails from the list where there is a matching User in Discourse
emails.each { |e| userlist << User.try(:where, email: e) } # the use of try avoids errors when no match is found

userlist.reject!(:&empty?) # get rid of any non-matches which are an empty entry in the array
userlist.flatten! # flatten the array by one level

# BadgeGranter requires the Badge object and the User object to be passed in as parameters
# In our case we were assigning the Badge with the id number 108
userlist.each {|u| BadgeGranter.grant ( Badge.find(108), u ) }

```

It seems to work and so far hasn’t caused any ‘undocumented features’ or killed the DB

M

---

<div class="post-metadata">

### Author: ![Mytee](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@Mytee](https://meta.discourse.org/u/Mytee)
#### Post date: [2015 年 12 月 17 日午前 5:35 UTC](https://meta.discourse.org/t/badge-granting-in-bulk-or-for-a-group-of-users/36782/2 "2015-12-17T05:35:31Z")

</div>

Sounds like a nice feature. Thanks for the code, and we’re also interested in having a similar feature.

---

<div class="post-metadata">

### Author: ![erlend\_sh](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/erlend_sh/32/119475_2.png) [@erlend\_sh](https://meta.discourse.org/u/erlend_sh)
#### Post date: [2015 年 12 月 18 日午後 12:04 UTC](https://meta.discourse.org/t/badge-granting-in-bulk-or-for-a-group-of-users/36782/3 "2015-12-18T12:04:00Z")

</div>

Another workaround would be to create a group dedicated to a single badge, and use the “[badge for all members of a group](https://meta.discourse.org/t/a-badge-for-all-members-of-a-group/18147)” query to give these members a badge.

---

<div class="post-metadata">

### Author: ![pacharanero](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pacharanero/32/500583_2.png) [@pacharanero](https://meta.discourse.org/u/pacharanero)
#### Post date: [2016 年 2 月 26 日午後 12:05 UTC](https://meta.discourse.org/t/badge-granting-in-bulk-or-for-a-group-of-users/36782/4 "2016-02-26T12:05:51Z")

</div>

> [@pacharanero](#):
>
> userlist.reject!(:&empty)

Note to self: the above code should be `userlist.reject!(&:empty?)`

---

<div class="post-metadata">

### Author: ![burke](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/burke/32/4211_2.png) [@burke](https://meta.discourse.org/u/burke)
#### Post date: [2018 年 8 月 27 日午後 5:37 UTC](https://meta.discourse.org/t/badge-granting-in-bulk-or-for-a-group-of-users/36782/5 "2018-08-27T17:37:11Z")

</div>

In the midst of manually assigning a couple of badges to a couple dozen people and missing a “list of people you want to grant this badge to” multiple user selector (+ optional reason) in the /admin/badges page for a badge. I know this is an old feature request, but wanted to acknowledge that I was looking for this feature today.

---

<div class="post-metadata">

### Author: ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)
#### Post date: [2020 年 1 月 20 日午後 3:46 UTC](https://meta.discourse.org/t/badge-granting-in-bulk-or-for-a-group-of-users/36782/6 "2020-01-20T15:46:34Z")

</div>

バッジの[一括付与](https://meta.discourse.org/t/award-a-badge-to-a-set-of-users/138694)ツールが新たに利用可能になりました。これが役立つかもしれません。

---

<div class="post-metadata">

### Author: ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)
#### Post date: [2020 年 1 月 20 日午後 3:46 UTC](https://meta.discourse.org/t/badge-granting-in-bulk-or-for-a-group-of-users/36782/7 "2020-01-20T15:46:44Z")

</div>


