# 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:** Product Support\
**Created:** [Octobre 17, 2016, 7:36 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:** 1\
**Showing post:** 9

<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:** [Mars 8, 2019, 9:16 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)

---

_[View the full topic](https://meta.discourse.org/t/new-category-automatically-as-muted-or-watching-for-all-users/51654)._
