How can I grant a badge to the first 500 users of my forum?


(Travis) #1

I want to grant a commemorative badge to the first 500 users of my forum (and maybe some other milestones). What’s the best way to do that?


What cool badge queries have you come up with?
(Jens Maier) #2

In the Rails console, this might work:

badge = Badge.find_by(name: 'Founding member')
User.where('id > 0').order(created_at: :asc).limit(500).each {|user| BadgeGranter.grant(badge, user) }

Alternatively, you can use this SQL query in the badge’s settings:

SELECT id AS user_id, created_at AS granted_at, NULL AS post_id
FROM users WHERE id > 0
ORDER BY created_at ASC LIMIT 500

(Travis) #3

Looks like that worked. Thanks!


(Michael Downey) #4

This query now generates the following error:

Contract violation:
Query triggers on users, but does not reference the ':user_ids' array

Any ideas to update it?

UPDATE: Never mind, I needed to change the Trigger from “When a user is edited or created” to “Update daily”.


(Joe Z) #5

Slight update that I’m using now. I have different levels of founding members, like first 100, next 400, next 1500…etc. I didn’t want to have the first 100 members be awarded all of the different levels, so came up with this. Limit 5000 is maximum member count (first 5000), then p.net > 2600 lists which users to skip…the first 2600 since they’d already have badges.

select p.* from (
SELECT id AS user_id, created_at AS granted_at, NULL AS post_id, ROW_NUMBER() OVER(ORDER BY created_at ASC) as num
FROM users WHERE id > 0
ORDER BY created_at ASC LIMIT 5000) p
where p.num > 2600

(Sami Syed) #6

This does not work anymore I just tried it and got the same error


(Daniela) #7

It works perfectly if the trigger is “Update daily”


(Sami Syed) #8

9
I get this error?


(Daniela) #9

Again, you must set the trigger ‘update daily’ to avoid the error


(Sami Syed) #10

I already did and I still get that error


(Daniela) #11

Do you use SSO on your site?


(Sami Syed) #12

yes but discourse controls the logins and signups, etc…

Will it still work?