# New category automatically as muted or watching for all users

**URL:** https://meta.discourse.org/t/new-category-automatically-as-muted-or-watching-for-all-users/51654
**Category:** Support
**Created:** [October 17, 2016, 7:36pm UTC](https://meta.discourse.org/t/new-category-automatically-as-muted-or-watching-for-all-users/51654 "2016-10-17T19:36:46Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Manfred\_Bayer-Lemerz](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/manfred_bayer-lemerz/32/62721_2.png) [@Manfred\_Bayer-Lemerz](https://meta.discourse.org/u/Manfred_Bayer-Lemerz)
#### Post date: [October 17, 2016, 7:36pm UTC](https://meta.discourse.org/t/new-category-automatically-as-muted-or-watching-for-all-users/51654/1 "2016-10-17T19:36:46Z")

</div>

When we add a new category it is automatically as normal for all user can we also set that has a different status like watching or muted?

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [October 17, 2016, 11:48pm UTC](https://meta.discourse.org/t/new-category-automatically-as-muted-or-watching-for-all-users/51654/2 "2016-10-17T23:48:55Z")

</div>

You can do that via the Rails console, but it’d be a manual command.

---

<div class="post-metadata">

### Author: ![fefrei](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fefrei/32/119538_2.png) [@fefrei](https://meta.discourse.org/u/fefrei)
#### Post date: [November 9, 2016, 12:40pm UTC](https://meta.discourse.org/t/new-category-automatically-as-muted-or-watching-for-all-users/51654/3 "2016-11-09T12:40:22Z")

</div>

How would that look like?

I’d assume it’s a loop over all users where for each user, something like

```ruby
CategoryUser.create!(user_id: user.id, <INSERT HERE>, CategoryUser.notification_levels[:muted])

```

is run – but my Ruby skills are quite lacking, and I assume this would crash if the user already had an entry here.

* * *

`CategoryUser.batch_set` is also looking promising…

* * *

Would

```ruby
User.all.each do |user| CategoryUser.batch_set(user, :muted, [7]) end

```

do the trick (for category 7)?

---

<div class="post-metadata">

### Author: ![fefrei](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fefrei/32/119538_2.png) [@fefrei](https://meta.discourse.org/u/fefrei)
#### Post date: [November 9, 2016, 1:52pm UTC](https://meta.discourse.org/t/new-category-automatically-as-muted-or-watching-for-all-users/51654/4 "2016-11-09T13:52:43Z")

</div>

After some toying around, it looks like my last command did work 🙂

---

<div class="post-metadata">

### Author: ![muppeth](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/muppeth/32/44709_2.png) [@muppeth](https://meta.discourse.org/u/muppeth)
#### Post date: [January 8, 2017, 8:17pm UTC](https://meta.discourse.org/t/new-category-automatically-as-muted-or-watching-for-all-users/51654/5 "2017-01-08T20:17:54Z")

</div>

yup. It does, however it removes all other already muted category. Is there a way to append new muted category instead?

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [January 17, 2017, 3:07pm UTC](https://meta.discourse.org/t/new-category-automatically-as-muted-or-watching-for-all-users/51654/6 "2017-01-17T15:07:07Z")

</div>

Not following, can you give me a concrete example of what you are trying to do.

---

<div class="post-metadata">

### Author: ![ChrisBeach](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/chrisbeach/32/214628_2.png) [@ChrisBeach](https://meta.discourse.org/u/ChrisBeach)
#### Post date: [January 19, 2017, 3:37am UTC](https://meta.discourse.org/t/new-category-automatically-as-muted-or-watching-for-all-users/51654/7 "2017-01-19T03:37:58Z")

</div>

Along similar lines, I’m using groups to allow members to opt in to see [special-interest categories](https://se23.life/groups).

Ideally, upon opting in to such a category, members would be “watching first post” on that category. I’m not sure if/how I can configure this.

---

<div class="post-metadata">

### Author: ![muppeth](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/muppeth/32/44709_2.png) [@muppeth](https://meta.discourse.org/u/muppeth)
#### Post date: [January 23, 2017, 11:25pm UTC](https://meta.discourse.org/t/new-category-automatically-as-muted-or-watching-for-all-users/51654/8 "2017-01-23T23:25:31Z")

</div>

Sorry for late answer.

Here’s my usecase.  
I would like every new category to be set to “mute” for every user, so that they need to unmute it if they want to get notifications (specially when using mailinglist mode).  
In admin settings under “User preferences” I added all the categories I want to have “muted” by default. Whenever there is newly created category I can add it to that list. This however covers only the new users that just created an account. I’m looking for a way to easily mark each new category for each existing user as “muted” by default.  
I used rail console query from above:  
`User.all.each do |user| CategoryUser.batch_set(user, :muted, [7]) end`  
but that instead of setting new category to “mute” only, overwrittes all muted categories users setup previously, leaving only this new category as muted.

I’m looking for query that would only add new category to the already setup list by users and not overwrite.

Does this make more sense? Sorry if I make it sound unlcear.

---

<div class="post-metadata">

### Author: ![pacharanero](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pacharanero/32/500583_2.png) [@pacharanero](https://meta.discourse.org/u/pacharanero)
#### Post date: [March 8, 2019, 9:16am UTC](https://meta.discourse.org/t/new-category-automatically-as-muted-or-watching-for-all-users/51654/9 "2019-03-08T09:16:24Z")

</div>

Hi @muppeth, apologies for bumping this thread but I’ve been doing some Rails Console Discourse Hacking recently on a similar topic and thought I might be able to offer a solution.

Your tried command `User.all.each do |user| CategoryUser.batch_set(user, :muted, [7]) end` will overwrite any existing `CategoryUser`s. This is not what you want. The `CategoryUser` object creates a link between a `User` and a `Category`, setting the relationship between the two. If a user wants to mute (or watch, etc) 2 categories, then there will be 2 `CategoryUsers` with that `user_id`. The above command will overwrite every single one of them to mute the same Category, whatever they were set to before.

Here is some Ruby which checks to see if a CategoryUser exists for that user\_id and category\_id (leaving it alone in that case, allowing the user’s existing preference to be honoured)

In this case I wanted a list of users from a group, but you could do `Users.all`

```ruby
# get a list of users
users = User.joins(:groups).where(groups: {name: "MyGroupName"})

# where category_id is the ID of the category you want to mute
users.each { |u| unless CategoryUser.exists?(user_id: u.id, category_id: 8) then CategoryUser.create(user_id: u.id, category_id: 8, CategoryUser.notification_levels[:muted]) end }

```

You would need to do this for each new Category you create whch you want to have muted

If you need this to be applied for all new users as they join, then there is a plugin which sets up a scheduled job to do this, which you could adapt to set muting daily for all new users, using similar commands.  
[https://github.com/discourse/discourse-watch-category-mcneel](https://github.com/discourse/discourse-watch-category-mcneel)

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [April 20, 2022, 12:07pm UTC](https://meta.discourse.org/t/new-category-automatically-as-muted-or-watching-for-all-users/51654/10 "2022-04-20T12:07:24Z")

</div>



---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [April 20, 2022, 12:26pm UTC](https://meta.discourse.org/t/new-category-automatically-as-muted-or-watching-for-all-users/51654/11 "2022-04-20T12:26:16Z")

</div>

You can now set category notification defaults in the admin settings for Watching, Watching First Post, Tracking, Normal, and Muted. You can pull them all up by searching for `default categories` in your admin search box.

There’s also a choice for specific groups too. You can find that in the `manage/categories` tab on the groups’ page. 👍

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [October 27, 2022, 10:10am UTC](https://meta.discourse.org/t/new-category-automatically-as-muted-or-watching-for-all-users/51654/12 "2022-10-27T10:10:06Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
