Discourse Gamification - Getting 404 / MissingAttributeError on /leaderboard route

So, I recently decided to enable gamification and immediately ran into an issue where the /leaderboard route was returning a 404 error. The backend had over 800k score records calculated and the permissions checked out, but the page itself wouldn’t load.

Checking the server logs showed:
ActiveModel::MissingAttributeError (missing attribute 'admin' for User)

While digging through the plugin code, I noticed in lib/discourse_gamification/leaderboard_cached_view.rb that the user query uses a specific .select string to pull columns for the leaderboard:

.select(
"users.id, users.name, users.username, users.uploaded_avatar_id, p.total_score, p.position",
)

Because users.admin and users.moderator aren’t included in that query, the instantiated user objects don’t have those fields available. It looks like (?) somewhere further down the line during serialization or core layout rendering, a staff check is being triggered on those users, causing Rails to throw the MissingAttributeError and fall back to a 404.

I manually edited that file to include users.admin and users.moderator in the .select block:

.select(
  "users.id, users.name, users.username, users.uploaded_avatar_id, users.admin, users.moderator, p.total_score, p.position",
)

After restarting the app container, the leaderboard started loading perfectly.

I’m guessing this is a bug (?) or have I completely overthought/overlooked something?

The site is currently on v2026.6.0-latest (47a830330f)

2 Likes

The error and the explanation you provided match. Unfortunately I was unable to reproduce the issue from 47a830330f or on main. I tried it signed in as an admin and as a regular user, and they both load fine.

As a sanity check I just want to confirm you’re using the version of gamification that’s bundled with Discourse core, and doesn’t have the old, stand-alone plugin installed?

Are you able to provide more of the stack trace of where the MissingAttributeError is happening?

1 Like

@ted,

Actually, you can feel free to disregard this. I ultimately traced the issue back to a plugin that hooked into the UserScoreSerializer that implemented its own narrow .select() statement. It wasn’t apparent initially because there was no backtrace provided in /logs for the MissingAttributeError. Only after digging through and making the call manually through rails to get a full stack trace was I able to pinpoint the errant plugin.

Thanks, and sorry for the false alarm!

2 Likes

No problem! You had an issue and you posted about it. Glad you got it sorted. :slightly_smiling_face: