# Watching Categories based on on group in discourse

**URL:** https://meta.discourse.org/t/watching-categories-based-on-on-group-in-discourse/41488
**Category:** Development
**Created:** [Mars 23, 2016, 6:37 UTC](https://meta.discourse.org/t/watching-categories-based-on-on-group-in-discourse/41488 "2016-03-23T18:37:39Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Jared\_Needell](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jared_needell/32/116554_2.png) [@Jared\_Needell](https://meta.discourse.org/u/Jared_Needell)
#### Post date: [Mars 23, 2016, 6:37 UTC](https://meta.discourse.org/t/watching-categories-based-on-on-group-in-discourse/41488/1 "2016-03-23T18:37:40Z")

</div>

I found this plugin

> <https://github.com/discourse/discourse-watch-category-mcneel/blob/main/plugin.rb>

Is this only hardcoded for one category and one group? I’d like to try and set this up on a larger scale.

@tobiaseigen

---

<div class="post-metadata">

### Author: ![tobiaseigen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tobiaseigen/32/539204_2.png) [@tobiaseigen](https://meta.discourse.org/u/tobiaseigen)
#### Post date: [Mars 23, 2016, 6:39 UTC](https://meta.discourse.org/t/watching-categories-based-on-on-group-in-discourse/41488/2 "2016-03-23T18:39:51Z")

</div>

What you want to do is fork the plugin on github, update your forked version according to your needs, and then include your forked version in app.yml. I’ve got it working now for I think 5 categories.

My version - to give you an example.

> <https://github.com/namati/discourse-watch-category-mcneel/blob/master/plugin.rb>

---

<div class="post-metadata">

### Author: ![Jared\_Needell](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jared_needell/32/116554_2.png) [@Jared\_Needell](https://meta.discourse.org/u/Jared_Needell)
#### Post date: [Mars 23, 2016, 6:53 UTC](https://meta.discourse.org/t/watching-categories-based-on-on-group-in-discourse/41488/3 "2016-03-23T18:53:27Z")

</div>

Ok sweet

So for your code just make sure I’m reading this right

Namati\_Staff gets subscribed to Annoucements  
Namati\_Leadership get subscribed to Leadership  
Network\_NGC gets subscribed to NGC  
Namati\_Culture get subscribed to Culture  
namati\_assessingopp gets subscribed to assessingopp  
namati\_scale gets subbed to scale

If that’s right I’m gonna start testing this out, it would be cool to build out a UI for this.

---

<div class="post-metadata">

### Author: ![tobiaseigen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tobiaseigen/32/539204_2.png) [@tobiaseigen](https://meta.discourse.org/u/tobiaseigen)
#### Post date: [Mars 23, 2016, 7:19 UTC](https://meta.discourse.org/t/watching-categories-based-on-on-group-in-discourse/41488/4 "2016-03-23T19:19:15Z")

</div>

yup - you’ve got it right.

---

<div class="post-metadata">

### Author: ![Jared\_Needell](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jared_needell/32/116554_2.png) [@Jared\_Needell](https://meta.discourse.org/u/Jared_Needell)
#### Post date: [Mars 23, 2016, 7:54 UTC](https://meta.discourse.org/t/watching-categories-based-on-on-group-in-discourse/41488/5 "2016-03-23T19:54:17Z")

</div>

So testing this out

I have a EmployeesOnly group and a category called : “CONFIDENTIALEmployees Only”

 ![](https://global.discourse-cdn.com/meta/original/3X/e/a/ea1dd7da097bbbd2e480bdcd808056f9ca11703e.png)

 ![](https://global.discourse-cdn.com/meta/original/3X/2/f/2fe345f6cc563542cb71c0ebe23405557744a2b9.png)

So I want everyone in that group subscribed to this category,

I forked the code and tweaked it to this…

> <https://github.com/tekwreck/discourse-watch-category-mcneel/blob/master/plugin.rb>

Added the plugin to app.yml , rebuilt

I can see this plugin in admin panel

 ![](https://global.discourse-cdn.com/meta/original/3X/d/6/d61c1c61dbec57c4f22c2fdfa2a552cd73cbed50.png)

And finally I went to sidekiq to manually trigger the job,

 ![](https://global.discourse-cdn.com/meta/original/3X/a/9/a98d8afb9be9a0dbff1bda9ccf91c851c8cb09d4.png)

My account has permission to see this category and is a member of of the EmployeesOnly group. Even with triggering the job, my account isn’t being set to watch “CONFIDENTIALEmployees Only”

Any way to see an error log of some type for this?

---

<div class="post-metadata">

### Author: ![Jared\_Needell](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jared_needell/32/116554_2.png) [@Jared\_Needell](https://meta.discourse.org/u/Jared_Needell)
#### Post date: [Mars 23, 2016, 8:52 UTC](https://meta.discourse.org/t/watching-categories-based-on-on-group-in-discourse/41488/6 "2016-03-23T20:52:42Z")

</div>

Tried with the everyone group as well and doesn’t work.

---

<div class="post-metadata">

### Author: ![tobiaseigen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tobiaseigen/32/539204_2.png) [@tobiaseigen](https://meta.discourse.org/u/tobiaseigen)
#### Post date: [Mars 23, 2016, 9:18 UTC](https://meta.discourse.org/t/watching-categories-based-on-on-group-in-discourse/41488/7 "2016-03-23T21:18:54Z")

</div>

I just tested as well and can confirm that this plugin no longer works.

@techAPJ is it possible this plugin of yours is outdated, now that user settings have moved to the user options table? This is not a priority for me right now - perhaps you or @Jared_Needell could update the plugin.

[https://meta.discourse.org/t/edit-a-user-setting-for-all-discourse-users/25162/7?u=tobiaseigen](https://meta.discourse.org/t/edit-a-user-setting-for-all-discourse-users/25162/7)

---

<div class="post-metadata">

### Author: ![Jared\_Needell](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jared_needell/32/116554_2.png) [@Jared\_Needell](https://meta.discourse.org/u/Jared_Needell)
#### Post date: [Mars 23, 2016, 10:25 UTC](https://meta.discourse.org/t/watching-categories-based-on-on-group-in-discourse/41488/8 "2016-03-23T22:25:33Z")

</div>

@codinghorror might be able to provide insight on this too.

---

<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: [Mars 23, 2016, 10:26 UTC](https://meta.discourse.org/t/watching-categories-based-on-on-group-in-discourse/41488/9 "2016-03-23T22:26:25Z")

</div>

@techapj worked on it – he can elaborate.

---

<div class="post-metadata">

### Author: ![Jared\_Needell](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jared_needell/32/116554_2.png) [@Jared\_Needell](https://meta.discourse.org/u/Jared_Needell)
#### Post date: [Mars 24, 2016, 12:59 UTC](https://meta.discourse.org/t/watching-categories-based-on-on-group-in-discourse/41488/10 "2016-03-24T00:59:20Z")

</div>

So teaching myself rails tonight!

Breaking down the plugin I did some testing.

Category.find\_by\_slug(“site-feedback”) doesn’t seem to work, I get a nil return

but if I do

Category.find\_by(slug: “site-feedback”) appears to return data.

 ![](https://global.discourse-cdn.com/meta/original/3X/1/4/1417427d91fa0ee86f303d9244f49df92c5a2d51.png)

I made these changes on the plugin and appears to working..

Here’s my code, I’m going to be updating / tweaking it through the night to fit my site needs.

> <https://github.com/tekwreck/discourse-watch-category-mcneel/blob/master/plugin.rb>

---

<div class="post-metadata">

### Author: ![Jared\_Needell](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jared_needell/32/116554_2.png) [@Jared\_Needell](https://meta.discourse.org/u/Jared_Needell)
#### Post date: [Mars 24, 2016, 1:24 UTC](https://meta.discourse.org/t/watching-categories-based-on-on-group-in-discourse/41488/11 "2016-03-24T01:24:44Z")

</div>

Plugin also supports looping through all users just not groups. Helpful when it comes to enforcing notifications

---

<div class="post-metadata">

### Author: ![watchmanmonitor](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/watchmanmonitor/32/430970_2.png) [@watchmanmonitor](https://meta.discourse.org/u/watchmanmonitor)
#### Post date: [Mars 24, 2016, 4:38 UTC](https://meta.discourse.org/t/watching-categories-based-on-on-group-in-discourse/41488/12 "2016-03-24T04:38:55Z")

</div>

One day this functionality will be in Discourse core, and I will be even happier with the product.

See also

> [@Override existing user preferences](https://meta.discourse.org/t/override-existing-user-preferences/40966/7):
>
> Good points @Mittineague but where I need this is in cases where: there is a new feature in discourse and I need to set its value for everyone retroactively if I add a new Category and want it to be in everyone’s Watch list by default (they can unfollow if they want) if we are generally changing how the forum works for whatever internal politicking reason. In all of these cases, the need here is to be able to Fix Our Forum for our users. If I expected to have to use this feature regularly on…

and even more relevant:

> [@Make members of a group watch a category by default](https://meta.discourse.org/t/make-members-of-a-group-watch-a-category-by-default/22127/):
>
> We have a private category, limited to a particular opt-in group of Discourse members for our organization. Say it’s “staff” – it’s not actually, but it’s along those lines. I want to make sure that all staff receive prompt notifications of discussions in that private category. We encourage them to set their category settings to “watching” but I don’t think many of them have. Under the category’s security settings, when I say “staff can create/reply/see” I’d love it if there were a way to sa…

---

<div class="post-metadata">

### Author: ![Jared\_Needell](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jared_needell/32/116554_2.png) [@Jared\_Needell](https://meta.discourse.org/u/Jared_Needell)
#### Post date: [Mars 24, 2016, 12:21 UTC](https://meta.discourse.org/t/watching-categories-based-on-on-group-in-discourse/41488/13 "2016-03-24T12:21:18Z")

</div>

Agreed, there’s a lot of features that would make me even happier 🙂

Luckily for a first experience debugging a rails plugin it wasn’t too complicated.

---

<div class="post-metadata">

### Author: ![downey](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/downey/32/166878_2.png) [@downey](https://meta.discourse.org/u/downey)
#### Post date: [Mars 25, 2016, 2:03 UTC](https://meta.discourse.org/t/watching-categories-based-on-on-group-in-discourse/41488/14 "2016-03-25T02:03:23Z")

</div>

Any chance you can submit a pull request back with your fixes? 🙂

---

<div class="post-metadata">

### Author: ![Jared\_Needell](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jared_needell/32/116554_2.png) [@Jared\_Needell](https://meta.discourse.org/u/Jared_Needell)
#### Post date: [Mars 25, 2016, 3:35 UTC](https://meta.discourse.org/t/watching-categories-based-on-on-group-in-discourse/41488/15 "2016-03-25T03:35:11Z")

</div>

If you notice, I changed it quite a bit utilizing methods to reduce repetitive code. I might take advantage of that and see if there is some way I can incorporate a UI to this.

I can do the pull request but I still plan on making more changes.

---

<div class="post-metadata">

### Author: ![downey](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/downey/32/166878_2.png) [@downey](https://meta.discourse.org/u/downey)
#### Post date: [Mars 25, 2016, 4:16 UTC](https://meta.discourse.org/t/watching-categories-based-on-on-group-in-discourse/41488/16 "2016-03-25T04:16:50Z")

</div>

That’s awesome. Keep up the great work, this could make a big impact! 🙂

---

<div class="post-metadata">

### Author: ![techAPJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/techapj/32/342990_2.png) [@techAPJ](https://meta.discourse.org/u/techAPJ)
#### Post date: [Avril 11, 2016, 5:44 UTC](https://meta.discourse.org/t/watching-categories-based-on-on-group-in-discourse/41488/17 "2016-04-11T17:44:54Z")

</div>

> [@Jared\_Needell](#):
>
> Category.find\_by\_slug(“site-feedback”) doesn’t seem to work, I get a nil return
> 
> but if I do
> 
> Category.find\_by(slug: “site-feedback”) appears to return data.

This is because I added a `find_by_slug` method in `Category` model:

> <https://github.com/discourse/discourse/blob/be3a5a56ccc284b352aa65080fc6b955f73cc72a/app/models/category.rb#L454-L462>

As you can see in above snippet, this method requires both `category_slug` and `parent_category_slug`, so you will have to pass `parent_category_slug` as well if applicable.

---

<div class="post-metadata">

### Author: ![Jared\_Needell](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jared_needell/32/116554_2.png) [@Jared\_Needell](https://meta.discourse.org/u/Jared_Needell)
#### Post date: [Avril 11, 2016, 5:58 UTC](https://meta.discourse.org/t/watching-categories-based-on-on-group-in-discourse/41488/18 "2016-04-11T17:58:35Z")

</div>

Doesn’t look like it requires the parent, the method you linked has an if statement to proceed without the parent category if it isn’t provided.

---

<div class="post-metadata">

### Author: ![cpradio](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cpradio/32/4970_2.png) [@cpradio](https://meta.discourse.org/u/cpradio)
#### Post date: [Avril 11, 2016, 6:01 UTC](https://meta.discourse.org/t/watching-categories-based-on-on-group-in-discourse/41488/19 "2016-04-11T18:01:28Z")

</div>

That likely handles top-level categories only. If you want a sub-category, it seems the parent category is required.

---

<div class="post-metadata">

### Author: ![techAPJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/techapj/32/342990_2.png) [@techAPJ](https://meta.discourse.org/u/techAPJ)
#### Post date: [Avril 11, 2016, 6:13 UTC](https://meta.discourse.org/t/watching-categories-based-on-on-group-in-discourse/41488/20 "2016-04-11T18:13:23Z")

</div>

In case the `parent_category_slug` is not passed the method will look for top level categories only, as per this query:

```plaintext
self.where(slug: category_slug, parent_category_id: nil).first

```

In your case I can see from console screenshot that `parent_category_id` is `5`, so the above condition will not be satisified, hence the method returns `nil`.

[Next page](https://meta.discourse.org/t/watching-categories-based-on-on-group-in-discourse/41488.md?page=2)
