Episodes
David Kimura PRO said over 1 year ago on Removing Friendly Id :
  you might be able to check if the string contains a UUID and escape the generate_slug method.

someone PRO said over 1 year ago on Removing Friendly Id :
So in my app I'm using the slugs to represent titles in posts instead of showing the post numbers. I had originally asked how to stop the UUID from changing each time a duplicate record was updated because it would break bookmarks.  I deleted the question though because I foolishly thought at first that all I had to do was remove "before_update :generate_slug" from sluggable.rb.  But as I later realized this may stop a UUID from changing when the duplicate post title gets updated, however it will not change the title in the url anymore.  So if someone were to change the title of a post from "XXX" to "YYY" it would still show the old "XXX" in the url even though the post itself had the new title.  

So after a little digging I found this simple fix that checks if the title field has been changed and only runs the generate_slug function if it has.  basically you just need to append "if: <some_field>_changed?" to any field you want to check.  This now allows for duplicate titles that have UUID's to keep their original UUID when updating unless the title has explicitly been changed to a new title, the UUID might be deleted if it no longer is a duplicate or a new UUID is generated only if it is a duplicate of a different title.   

    before_update :generate_slug, if: :title_changed? 

     

Login to Comment