502 error when trying to save the categories order

Hi! I have a lot of categories (about 2000). Now I need to change the order for 1 category. The standard “Reorder Categories” function fails with 502 error after 1 minute. How can this be solved? Thank you!

Have you been able to sort this out? If you are still getting timeout errors, you might have to set the category’s position from the site’s Rails console. That seems like it could be somewhat complicated to do with 2000 categories though.

No, haven’t found the solution yet.

How can I do this? I have no idea about the Rails console.

I looked into this a little more. With 2000 categories, it is going to be a fairly complicated operation - changing the position of one category will affect the position of all categories that come after it. I’m reluctant to suggest making the change through the console.

Technically, I’m not sure if this is a bug. Discourse does allow you to create 2000+ categories, but the UI isn’t designed to handle that many categories. Have you been able to re-order categories in the past without issues?

2 Likes

Do you want them alphabetical? I once had a script to sort categories and subcategories in alphabetical order.

I suppose someone already tried to talk you into using tags instead? This is likely just the first of the issues you’ll have with this many categories.

2 Likes

Nope, I face this problem the 1st time and hadn’t it before. I created this amount of categories through the script in the normal interface. How can I do it through the console?

I use tags for another purpose and can’t mix them with other substances. Now I need to move 1 subcategory up. I sorted them manually before creation. The only way is to use a console. Any ideas how to do it?

You’d do something like

cd /var/discourse
./launcher enter app
rails c

to get to the console. You’d then do stuff

If you don’t understand them, then you probably don’t want to do it yourself.

Here are scripts that I believe worked on or about Jan 12, 2018. I don’t promise that they work, but I don’t see why not. Take backups and light a candle. :wink:

# alpha sort all categories matching search and their sub-categories

  def sort_matching_categories_and_subcategories(search)
    categories = Category.where("name like ?", search)
    position = 100
    categories.order(:name).each do |cat|
      position += 5
      cat.position = position
      cat.save!()
      c_position = 0
      children = Category.where(:parent_category_id=>cat.id)
      children.order(:name).each do |c|
        c_position += 5
        c.position = c_position
        c.save!()
      end
    end
  end


 # alpha sort subcategories of a single category matching search
def sort_matching_subcategories(search)
  categories = Category.where("name like ?", search)
  if categories.count > 1
    puts "Found more than one category"
  end
  categories.order(:name).each do |cat|
    c_position = 5
    children = Category.where(:parent_category_id=>cat.id)
    children.order(:name).each do |c|
      c_position += 5
      c.position = c_position
      c.save!()
    end
  end
end

If you need more help than that and have a budget, see Discourse Support — Literate Computing, LLC or send me an email.

I hope this helps!

2 Likes