How to award a non-custom badge through the console


(Simon Cossar) #1

There are rare cases where one of the default Discourse badges may need to be awarded to a user through the rails console. As an example: a user who has not been granted the Enthusiast badge, despite having visited a forum for 10 consecutive days. This can happen due to time zone differences between a user’s local time and the server’s UTC time.

First, make sure that the badge won’t be automatically revoked by the system after it has been granted. You can find that list of badge names by running the following in your rails console:

Badge.where(auto_revoke: false).pluck(:name)

That will give you the following list:

  • “First Flag”
  • “Thank You”
  • “Gives Back”
  • “Empathetic”
  • “Out of Love”
  • “Higher Love”
  • “Crazy in Love”
  • “Aficionado”
  • “Devotee”
  • “Enthusiast”
  • “Reader”
  • “Anniversary”
  • “Appreciated”
  • “Respected”
  • “Admired”

To grant a badge from this list, find the user you wish to grant the badge to:

user = User.find_by(username: 'enthusiastic_user')

Then find the badge by its name:

 badge = Badge.find_by(name: "Enthusiast")

Then grant the badge to the user:

BadgeGranter.grant(badge, user)