From 82bdfb9832e35d3b158be134c8c1a34127a2ba2d Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Thu, 17 Nov 2016 21:57:06 +0530 Subject: [PATCH] FIX: update category permalink if record already exists --- app/models/category.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/models/category.rb b/app/models/category.rb index 3a8cc580e2a..e0ac08ac877 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -467,9 +467,15 @@ SQL def create_category_permalink old_slug = changed_attributes["slug"] if self.parent_category - Permalink.create(url: "c/#{self.parent_category.slug}/#{old_slug}", category_id: id) + url = "c/#{self.parent_category.slug}/#{old_slug}" else - Permalink.create(url: "c/#{old_slug}", category_id: id) + url = "c/#{old_slug}" + end + + if Permalink.where(url: url).exists? + Permalink.where(url: url).update_all(category_id: id) + else + Permalink.create(url: url, category_id: id) end end