# 502 error when trying to save the categories order

**URL:** https://meta.discourse.org/t/502-error-when-trying-to-save-the-categories-order/184031
**Category:** Bug
**Created:** [March 22, 2021, 12:48pm UTC](https://meta.discourse.org/t/502-error-when-trying-to-save-the-categories-order/184031 "2021-03-22T12:48:21Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Ed\_Bobkov](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ed_bobkov/32/200014_2.png) [@Ed\_Bobkov](https://meta.discourse.org/u/Ed_Bobkov)
#### Post date: [March 22, 2021, 12:48pm UTC](https://meta.discourse.org/t/502-error-when-trying-to-save-the-categories-order/184031/1 "2021-03-22T12:48:21Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [March 24, 2021, 4:33pm UTC](https://meta.discourse.org/t/502-error-when-trying-to-save-the-categories-order/184031/2 "2021-03-24T16:33:15Z")

</div>

> [@Ed\_Bobkov](#):
>
> I have a lot of categories (about 2000). Now I need to change the order for 1 category.

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.

---

<div class="post-metadata">

### Author: ![Ed\_Bobkov](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ed_bobkov/32/200014_2.png) [@Ed\_Bobkov](https://meta.discourse.org/u/Ed_Bobkov)
#### Post date: [March 24, 2021, 5:21pm UTC](https://meta.discourse.org/t/502-error-when-trying-to-save-the-categories-order/184031/3 "2021-03-24T17:21:01Z")

</div>

No, haven’t found the solution yet.

> [@simon](#):
>
> you might have to set the category’s `position` from the site’s Rails console

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

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [March 24, 2021, 10:00pm UTC](https://meta.discourse.org/t/502-error-when-trying-to-save-the-categories-order/184031/4 "2021-03-24T22:00:32Z")

</div>

> [@Ed\_Bobkov](#):
>
> How can I do this?

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?

---

<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: [March 25, 2021, 12:29am UTC](https://meta.discourse.org/t/502-error-when-trying-to-save-the-categories-order/184031/5 "2021-03-25T00:29:10Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Ed\_Bobkov](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ed_bobkov/32/200014_2.png) [@Ed\_Bobkov](https://meta.discourse.org/u/Ed_Bobkov)
#### Post date: [March 25, 2021, 11:05am UTC](https://meta.discourse.org/t/502-error-when-trying-to-save-the-categories-order/184031/6 "2021-03-25T11:05:34Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Ed\_Bobkov](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ed_bobkov/32/200014_2.png) [@Ed\_Bobkov](https://meta.discourse.org/u/Ed_Bobkov)
#### Post date: [March 25, 2021, 11:07am UTC](https://meta.discourse.org/t/502-error-when-trying-to-save-the-categories-order/184031/7 "2021-03-25T11:07:42Z")

</div>

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?

---

<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: [March 25, 2021, 4:26pm UTC](https://meta.discourse.org/t/502-error-when-trying-to-save-the-categories-order/184031/8 "2021-03-25T16:26:09Z")

</div>

You’d do something like

```plaintext
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. 😉

```ruby
# 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 [Redirecting…](https://www.literatecomputing.com/discourse-support/) or send me an email.

I hope this helps!
