# Error moving sub category to top level with duplicate name

**URL:** https://meta.discourse.org/t/error-moving-sub-category-to-top-level-with-duplicate-name/31614
**Category:** Bug
**Created:** [31 ביולי,‏ 2015,‏ 4:21pm UTC](https://meta.discourse.org/t/error-moving-sub-category-to-top-level-with-duplicate-name/31614 "2015-07-31T16:21:36Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![anon21958287](https://avatars.discourse-cdn.com/v4/letter/a/ee59a6/32.png) [@anon21958287](https://meta.discourse.org/u/anon21958287)
#### Post date: [31 ביולי,‏ 2015,‏ 4:21pm UTC](https://meta.discourse.org/t/error-moving-sub-category-to-top-level-with-duplicate-name/31614/1 "2015-07-31T16:21:36Z")

</div>

On 1.4 Beta 6, I tried to move a category with a parent to be its own top level category. I get an error message in the dialog (“Sorry an error has occurred”). Chrome’s dev tools say:

`Failed to load resource: the server responded with a status of 422 (Unprocessable Entity)`

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [31 ביולי,‏ 2015,‏ 9:38pm UTC](https://meta.discourse.org/t/error-moving-sub-category-to-top-level-with-duplicate-name/31614/2 "2015-07-31T21:38:45Z")

</div>

> [@boomzilla](#):
>
> I tried to move a category with a parent to be its own top level category

What does this mean? Can you describe it in step by step fashion?

---

<div class="post-metadata">

### Author: ![anon21958287](https://avatars.discourse-cdn.com/v4/letter/a/ee59a6/32.png) [@anon21958287](https://meta.discourse.org/u/anon21958287)
#### Post date: [1 באוגוסט,‏ 2015,‏ 12:00am UTC](https://meta.discourse.org/t/error-moving-sub-category-to-top-level-with-duplicate-name/31614/3 "2015-08-01T00:00:43Z")

</div>

Click the “Edit” button on a category page. There’s a dropdown for “Parent Category.” I selected “no parent.”

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [1 באוגוסט,‏ 2015,‏ 12:10am UTC](https://meta.discourse.org/t/error-moving-sub-category-to-top-level-with-duplicate-name/31614/4 "2015-08-01T00:10:49Z")

</div>

Here’s the validation logic around parent categories:

```plaintext
  def parent_category_validator
    if parent_category_id
      errors.add(:base, I18n.t("category.errors.self_parent")) if parent_category_id == id
      errors.add(:base, I18n.t("category.errors.uncategorized_parent")) if uncategorized?

      grandfather_id = Category.where(id: parent_category_id).pluck(:parent_category_id).first
      errors.add(:base, I18n.t("category.errors.depth")) if grandfather_id
    end
  end

```

That shouldn’t trigger when making the parent nil.

We also have this:

```plaintext
  validates :name, if: Proc.new { |c| c.new_record? || c.name_changed? },
                   presence: true,
                   uniqueness: { scope: :parent_category_id, case_sensitive: false },
                   length: { in: 1..50 }

```

Is there, perhaps, [another top-level category](https://what.thedailywtf.com/c/general-help/coder-challenge) with [the same name?](https://what.thedailywtf.com/c/coder-challenge)

---

<div class="post-metadata">

### Author: ![anon21958287](https://avatars.discourse-cdn.com/v4/letter/a/ee59a6/32.png) [@anon21958287](https://meta.discourse.org/u/anon21958287)
#### Post date: [1 באוגוסט,‏ 2015,‏ 2:01am UTC](https://meta.discourse.org/t/error-moving-sub-category-to-top-level-with-duplicate-name/31614/5 "2015-08-01T02:01:48Z")

</div>

> [@riking](#):
>
> Is there, perhaps, another top-level category with the same name?

Ah…yes, there is. One was created when we imported the old stuff.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [1 באוגוסט,‏ 2015,‏ 2:21am UTC](https://meta.discourse.org/t/error-moving-sub-category-to-top-level-with-duplicate-name/31614/6 "2015-08-01T02:21:06Z")

</div>


