Bulk change of configuration via CLI in multiple user profiles/preferences

Yes, this can be done from the rails console. To get to the rails console, run ./launcher enter app, then enter rails c at the prompt to launch the rails console.

I’d be careful about looping through all users until you’re certain what’s going to happen. Here’s how I tested it for an individual user on my site:

First, get a list of the locales that are used by Discourse:

I18n.available_locales

That will print out a long list of symbols that Discourse uses for locales. For example :be, :bg, :bs_BA, :ca...

You can use these symbols to set the locale. For example, to update a user’s locale to French:

u = User.find(1)
u.update(locale: :fr)

If you want to loop through the users, you’ll need to figure out how to get arrays of users for each locale so that you can loop through them. If you have lists of usernames for each locale, that could be used to find the users. The simple way to do that from the console is to convert a list of usernames to an array. For example:

fr_users = ['bob', 'sally', 'john']

fr_users.each do |username|
    u = User.find_by(username: username)
    u.update(locale: :fr)
end

If you try this, proceed carefully. Make sure to create a backup of your site’s database before making the changes.

4 Likes