We ran into an issue while setting up our Discourse forum. We were trying to grant Moderator permission to an account we created for our Sales team, but kept receiving a 422 error.
I went through our error logs and this was showing up:
ActiveRecord::RecordInvalid (Validation failed: Name must be at least 6 characters)
/var/www/discourse/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.7.1/lib/active_record/validations.rb:79:in `raise_record_invalid'
We had modified the min username length to 6 instead of the default 3 when first going through the setup since that is the length we want to require. Once I reset the field to 3 I was able to add/remove the Moderator permissions. I did some testing and found that I could change min username length anywhere from 1-5 and it did not cause an issue, but as soon as it was set to 6 or greater it broke granting or removing Admin/Moderator permissions.
This didnât make sense to me since we only had 6 Users and all of their usernames were 6 characters or more. I looked through the rest of our forum to see if there was anything that would be related specifically to Moderators/Admins and had a name that was less than 6 characters. The staff Group is the only thing that Iâve been able to find that meets those criteria. And since it is 5 characters that may be the reason that I can set min username length up to 5 and not cause any issues. This definitely seems like something is not connected correctly within the system.
I found a couple of Topics here with similar issues:
The âsolutionâ to both of those issues was to just reset min username length to the default value, which is just a workaround not an actual fix for the issue.
Other than this issue the rest of the setup has been a breeze. Thanks for the great product.
For the users it breaks on, is there username less than 6? If so, that is why. Youâd have to convert them to users names greater than 6 before changing that minimum username setting.
Actually, maybe we should just make the minimum be 5 since staff is in the same namespace. It does seem a bit odd that group names are affecting this @sam, recategorizing this back to bugâŠ
I went ahead and added this softer warning instead, to both min and max:
min_username_length: "Minimum username length in characters. WARNING: if any existing users or groups have names shorter than this, your site will break!" max_username_length: "Maximum username length in characters. WARNING: if any existing users or groups have names longer than this, your site will break!"
How hard would it be to build a âCheck thisâ button into the admin interface?
i.e. when you click on âCheck thisâ it goes and scans for entries which would cause a problem, preferably returning a list of what would cause problems.
There is a similar issue with max post length. Even just a query with data explorer would be helpful (Iâm still getting to grips with data explorer).
@cpradio Good idea, that was the first thing I thought too when I saw the error in the logs. I triple checked all of the names to make sure that they were more than 6 characters.
@codinghorror Sorry it wasnât more obvious in my post that it was the Group name that appeared to be causing the issue. After looking at it again, I could have done a better job of highlighting that point since it was the main reason I wanted to point out the bug. Thanks for updating the help text as well. That should make it clearer that the field may have unwanted side affects if it is changed.
Long term I would prefer if we had a âvalidationâ routine that runs every time you try to increase length for this key:
if min_user_name_length.increased?
if User.where('length(username) < :size', size: min_user_name_length).exists?
raise "sorry, but this is not gonna happen"
end
if Group.where('length(name) < :size', size: min_user_name_length).exists?
raise "sorry, but this is also not gonna happen"
end
end
This is still an issue. Since âstaffâ is an existing group name, setting the min username length to anything other than 3, 4 or 5 will break granting and revoking admin if you do not rename the staff group first.
Worse, the error message is just âThe change you wanted was rejected. Maybe you tried to change something you didnât have access to.â and the logs show âName must be at least 6 charactersâ. Took me while to find out that this was about the group name (no actually, I never figured it out, I found this topic ).