Search code examples
ruby-on-railsrubyruby-on-rails-4friendly-id

Ruby on Rails - Remove slug uniqueness from Friendly_id slug in database


I use Friendly_id gem in my application. In my Slide model, I don't need slug to be unique, as it's never duplicated (it's auto generated)

How can I remove sluguniqueness from my slides database.

This is what I have: index_slides_on_slug_and_post_id and slug is :unique => true


Solution

  • if you want to do it in a rails migration, you should be able to remove the index and then add it back without the unique constraint.

    Something similar to this should do the trick

    class ChangeSlugIndex < ActiveRecord::Migration
      def change
        remove_index(:slides, name: 'index_slides_on_slug_and_post_id')
        add_index(:slides, [:slug,:post_id], name: 'index_slides_on_slug_and_post_id')
      end
    end