Ordering categories is quite glitched when the numbers get high

I’m trying to re-order (alphabetically) some subcategories. There are twenty subcats in the one category, and about 6 other categories, each with a few subcats.

When I click on an arrow icon to move a subcat around, it scrolls right down to the bottom most unhelpfully. Here is a screen recording of me struggling to do it:

2 Likes

Yeah. I once wrote some code to alphabetize them at the rails console, I think, but I don’t see it at hand.

You can probably get https://ask.discourse.com/ or some other AI to give you some code to do it. . .. Well, I couldn’t.

This might work, but I didn’t try it.

cats = Category.where("parent_category_id is not null").pluck(:parent_category_id).uniq

cats.each do |cat|
   subs = Category.where(parent_category_id: cat.id)
   subs_sorted = subs.sort_by { |c| c.name.downcase }
   pos = 10
   subs_sorted.each do |sub|
      sub.update(position: pos)
      pos += 10
   end
end
4 Likes