From f5a0e6a5fa09afd0e3984579ff23b2a67c23ff2c Mon Sep 17 00:00:00 2001 From: Ryan Siddle Date: Fri, 6 Jun 2014 12:20:12 +0100 Subject: [PATCH 1/2] Don't autofix for the Objects own ID When the model is updated, the current where clause checks against all permalinks for the model including its own ID which will always return true. Therefore it should be excluded as the model can override its own permalink. --- lib/has_permalink.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/has_permalink.rb b/lib/has_permalink.rb index 6c9d9a3..f6b2a2b 100644 --- a/lib/has_permalink.rb +++ b/lib/has_permalink.rb @@ -53,7 +53,7 @@ def to_param # Autofix duplication of permalinks def fix_duplication(permalink) if auto_fix_duplication - n = self.class.where(["permalink = ?", permalink]).count + n = self.class.where(["permalink = ? AND id != ?", permalink, id]).count if n > 0 links = self.class.where(["permalink LIKE ?", "#{permalink}%"]).order("id") From 3f4c830c15ad18f44ead37dbc4ae3b742cfa7326 Mon Sep 17 00:00:00 2001 From: Ryan Siddle Date: Wed, 11 Jun 2014 16:20:23 +0100 Subject: [PATCH 2/2] Fix for new records which don't have an ID yet. --- lib/has_permalink.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/has_permalink.rb b/lib/has_permalink.rb index f6b2a2b..4ae26f6 100644 --- a/lib/has_permalink.rb +++ b/lib/has_permalink.rb @@ -53,7 +53,11 @@ def to_param # Autofix duplication of permalinks def fix_duplication(permalink) if auto_fix_duplication - n = self.class.where(["permalink = ? AND id != ?", permalink, id]).count + n = if id.present? + self.class.where(["permalink = ? AND id != ?", permalink, id]).count + else + self.class.where(["permalink = ?", permalink]).count + end if n > 0 links = self.class.where(["permalink LIKE ?", "#{permalink}%"]).order("id")