The discussion revolves around updating category colors in bulk. bartv is looking for a way to easily “batch update” category colors, ideally setting a category and its children to a specified value. pfaffman suggests using the Rails console and offers to help with the code.
pfaffman provides a Ruby code snippet that makes child categories have the same color as their parents. The code needs to be pasted into the Rails console in two parts. bartv expresses gratitude for the solution.
The code provided is:
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
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.
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.
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.
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