Error moving sub category to top level with duplicate name



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)

(Jeff Atwood) #2

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


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

(Kane York) #4

Here’s the validation logic around parent categories:

  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

That shouldn’t trigger when making the parent nil.

We also have this:

  validates :name, if: { |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 with the same name?


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

(Jeff Atwood) #6