# How to mass update category colors?

**URL:** https://meta.discourse.org/t/how-to-mass-update-category-colors/86873
**Category:** Support
**Created:** [06.Май.2018 12:21:09 UTC](https://meta.discourse.org/t/how-to-mass-update-category-colors/86873 "2018-05-06T12:21:09Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![bartv](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bartv/32/130052_2.png) [@bartv](https://meta.discourse.org/u/bartv)
#### Post date: [06.Май.2018 12:21:10 UTC](https://meta.discourse.org/t/how-to-mass-update-category-colors/86873/1 "2018-05-06T12:21:10Z")

</div>

Hi,

after importing, we have a large number of categories that need new colors - it’s a bit of a Christmas tree right now and people are complaining 😉 How can I easily ‘batch update’ categories? The ideal solution would be to set a category and all its children to a specified value, but as a temporary work around setting all (sub-) categories to the same color will do too.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [06.Май.2018 14:37:16 UTC](https://meta.discourse.org/t/how-to-mass-update-category-colors/86873/2 "2018-05-06T14:37:16Z")

</div>

You see, people used to complain that they were all brown.

I would do it from the rails console. I think that I could come up with code to make all sub categories the same color as the parent without to much trouble.

---

<div class="post-metadata">

### Author: ![bartv](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bartv/32/130052_2.png) [@bartv](https://meta.discourse.org/u/bartv)
#### Post date: [06.Май.2018 14:40:03 UTC](https://meta.discourse.org/t/how-to-mass-update-category-colors/86873/3 "2018-05-06T14:40:03Z")

</div>

People always have something to complain, of course 😉 If you have some code laying around I’d love to see it! I don’t want to give you extra work though.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [07.Май.2018 20:20:02 UTC](https://meta.discourse.org/t/how-to-mass-update-category-colors/86873/4 "2018-05-07T20:20:02Z")

</div>

The below will make child categories have the same color as their parents. This needs to be pasted into the rails console in two parts. The first will put you in a viewer to see all the categories that it found, press `q` to quit, then paste the next part. I tested it on my copy of @bartv’s import and it performed as expected.

```ruby
cats=Category.where('parent_category_id > 0')

cats.each do |cat|
  parent = Category.find(cat.parent_category_id)
  cat.color = parent.color
  cat.save!
end

```

---

<div class="post-metadata">

### Author: ![bartv](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bartv/32/130052_2.png) [@bartv](https://meta.discourse.org/u/bartv)
#### Post date: [08.Май.2018 06:12:36 UTC](https://meta.discourse.org/t/how-to-mass-update-category-colors/86873/5 "2018-05-08T06:12:36Z")

</div>

Thanks so much Jay! 🙏

---

<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: [08.Июнь.2024 12:37:34 UTC](https://meta.discourse.org/t/how-to-mass-update-category-colors/86873/6 "2024-06-08T12:37:34Z")

</div>

Эта тема была автоматически закрыта через 2225 дней. Новые ответы больше не допускаются.
