Edit a user setting for all Discourse users


(Arpit Jalan) #1

So you want to edit user setting for all Discourse users? Great, let’s get started!

From console, run this command:

./launcher enter app

Access Rails console:

rails c

From Rails console, run the query:

To set the last seen date for users who have never logged in (never seen):

User.where("last_seen_at IS NULL").update_all(last_seen_at: 1.week.ago)

To default all users to mailing list mode (send an email for every new post):

UserOption.update_all(mailing_list_mode: true)

To default all users to email always mode (do not suppress email notifications when user is active on the site):

UserOption.update_all(email_always: true)

That’s it, you have edited user setting for all Discourse users!


Are digest emails sent to people who have no last seen date?
Default email settings for a new user
Possible to change joined date?
Safest way to overwrite existing user settings?
Watching a Category, perhaps the best kind of mailing list mode?
External links are not opening in new tab
Digest email not to be sent to new users
Is there a way to turn digests on for mass users?
Unsubscribe without unsubscribe link
What emails go to community and when?
Send Email to User When Mentioned
I need to change user email defaults after setup
Discourse Push Notifications
Mailing list question
Categories vs. Groups?
Keep a topic pinned permanently
How to disable the digest for users who already opted for it
Discourse Push Notifications
Default other enable quoting
Mailing list turned OFF - How to Disable Per Post Email for Existing Users
Bulk amend member digest subscriptions
Are digest emails sent to people who have no last seen date?
Did digest frequency for all users change with recent upgrade?
Setting defaults to enable mailing list mode-Temporary Solution
Mass-edit email of all users
Bulk change user settings
(Jesse Perry) #2

What’s the best method of going about finding the names to these settings?

For instance what if I wanted to change the “Don’t jump to my post after I reply” setting for everyone? I don’t, just for example, is there a good way to find the setting name in Discourse files somewhere?


(Mittineague) #3

The best place I’ve found so far is in the
/db/structure.sql file


(Kane York) #4

hmm? I’m not sure if that file’s actually used, I think it’s just kept as a reference for the result of the migrations.

ITYM using a psql console to change the column defaults.


(Jesse Perry) #5

Found this list (seems fairly comprehensive based off user prefs page):

'auto_track_topics_after_msecs',
                           'bio_raw',
                           'website',
                           'location',
                           'name',
                           'locale',
                           'email_digests',
                           'email_direct',
                           'email_always',
                           'email_private_messages',
                           'dynamic_favicon',
                           'digest_after_days',
                           'new_topic_duration_minutes',
                           'external_links_in_new_tab',
                           'mailing_list_mode',
                           'enable_quoting',
                           'disable_jump_reply',
                           'custom_fields',
                           'user_fields',
                           'muted_usernames'

From: /app/assets/javascripts/discourse/models/user.js.es6


(Krister Viirsaar) #6

Sorry to wake this long dead topic, but I used the User.update_all(mailing_list_mode: true) command - worked great. It worked so great that users mailboxes are flooded and I can’t switch it back anymore! Mailbox is going crazy.

But what’s really kicking my head in is that

User.find_by_username('Krister_Viirsaar').mailing_list_mode

returns false. That means the User.update_all(mailing_list_mode: false) command does work. BUT if I go to my settings the tick is still there and I still get all the emails ever.

What the hell is going on, where is Discourse really keeping this data?


(Sam Saffron) #7

It was moved to the user options table, next time you run migrations the old columns will be dropped

user.user_option.update_columns(mailing_list_mode: false)

Watching Categories based on on group in discourse
(Krister Viirsaar) #8

wow quick reply. thanks.

The command you gave does autocomplete in the terminal, but executing it produces error. I tried with both user and User

> user.user_option.update_columns(mailing_list_mode: false)
NameError: undefined local variable or method `user' for main:Object
from (pry):4:in `__pry__'

> User.user_option.update_columns(mailing_list_mode: false)
NoMethodError: undefined method `user_option' for #<Class:0x007f0e2102b968>
from /var/www/discourse/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.5.2/lib/active_record/dynamic_matchers.rb:26:in `method_missing'


(Krister Viirsaar) #9

Okay I think I got the hang of it.

User.find_by_username('Krister_Viirsaar').user_option.update_column('mailing_list_mode', false)

Just need to figure out how to apply this to all users now.

edit: ok got it (formatted for the command line oneliner)

User.find_each do |u|; u.user_option.update_columns(mailing_list_mode: false); end

Great first introduction to rails :smiley:


(Mittineague) #10

I call it “ActiveRecord” syntax. A bit different from other SQL syntax, but not all that hard to get used to.


(Sam Saffron) #11

To update all use

UserOption.update_all(col: val)

You don’t want to iterate through every record


(Krister Viirsaar) #12

as I said update_all updated the wrong thing unfortunately. I didn’t have many records anyway.


(Sam Saffron) #13

Had a typo there the table is user_options, updated


(Tobias Eigen) #14

Is the OP in this topic still correct and accurate? I am feeling a bit confused about the differences between the OP by @techAPJ and later posts by @sam like this one:

This morning I’d like to do the following three things:

  1. see a list of all users on my site and their digest frequency setting
  • see one user and their digest frequency
  • set the digest frequency to “weekly” for all users

I’d be grateful for up to date steps to do these things to help me along. :hatching_chick:


(Sam Saffron) #15

Step 1

Get data explorer installed then you can query stuff on the tables


(Jared Needell) #16

Or setup docker to expose psql and use PGadmin :slight_smile:


(Jay Pfaffman) #17

Perhaps @techAPJ or @sam should edit the OP with updates now that options are moved to a new table?

But what I really want to know is how can I apply settings to members of a group?


(Arpit Jalan) #18

Okay, done.

You will have to loop through group users, like:

beta_group = Group.find_by_name("beta")
beta_group.users.each do |user|
  UserOption.where(user_id: user.id).update_all(mailing_list_mode: true)
end

(Alex Armstrong) #19

I’d like to change everyone’s settings to this:

Capto_Capture 2017-12-14_15-40-51_

I’m not looking the answer, necessarily. I grok Ruby somewhat and can navigate GitHub so this can be a teach a man to fish moment :slight_smile:

But I’m not sure what setting to change using the OP’s code.


(Régis Hanol) #20

You need to change the notification_level_when_replying user option to watching :wink:

I’ll let you figure out the query. Let me know if you need help.