Modify trust level for all users

Here’s an example of a bulk operation to set the trust level to 2 for all users currently at trust level 0 or 1:

This is slow, but properly triggers all side effects (group membership updates, badge grants, and logging). It may be preferred especially when you have fewer than 1000 users. Note: this does not skip users with manually locked trust levels — add .where(manual_locked_trust_level: nil) to the query if you want to skip them.

User.where(trust_level: [0, 1]).find_each do |user|
   user.change_trust_level!(TrustLevel[2])
end

If there were more than 1000 users, I would have used this instead, but this bypasses all side effects (group updates, badges, logging) and does not respect locked trust levels.

DB.exec("UPDATE users SET trust_level = 2 WHERE trust_level IN (0, 1)")

Last edited by @southpaw 2025-01-29T15:02:09Z

Check documentPerform check on document:
22 Likes