How to mass update category colors?

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 :wink: 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.

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.

3 Likes

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

3 Likes

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.

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
9 Likes

Thanks so much Jay! :pray:

1 Like