Question

I'm trying to bypass the default friendly_id method which validates uniq value on a particular slug. In short, I would like to bypass friendly_id checking for slugs with same value and for it to not append the --1 , --2, etc. I need the ability to create records with friendly_id which can all have the same slug text.

        class Product < ActiveRecord::Base
         extend FriendlyId
         friendly_id :title, use: :slugged # use the title column to create slug column
Was it helpful?

Solution

I think the only way to do that would be to use scopes to allow each duplicate friendly_id to exist in its own "scope space".

https://github.com/norman/friendly_id/blob/master/lib/friendly_id/scoped.rb#L7

I'm guessing something like this (assuming that the duplicate friendly_ids belong to different userss):

class Product < ActiveRecord::Base
  extend FriendlyId
  friendly_id :tite, :use => [:scoped,:slugged], :scope => :user
end

OTHER TIPS

Thanks for the great answer, the scope solution is definitely the correct path. Although, just to help others, it really wasn't needed to do a full scope on the user object. I simply just did this:

     friendly_id :title, :use => :scoped, :scope => :id

and put the scope on the id in the table for the record. Since each id is unique, the slug will be the same and not append the --1, etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top