0

I am using the friendly_id gem version 3.3.3.0 in Rails 3.2

I have defined friendly id like following :-

class User < ActiveRecord::Base  
  has_friendly_id :name, use: :slugged, sequence_separator: '_', max_length: 32

end

I have used sequence separator as _

but when I execute query on console it was separating using _(underscore) but it works fines with --(double dash)

I have a 3 user with cached_slug as user_1 user_2 and user_3

when I am trying to find records with cached slug it generates following query.

using - _(underscore)

for - `user_1`
User.find("user_1")
SELECT sluggable_id FROM slugs WHERE ((slugs.sluggable_type = 'User' AND slugs.name = 'user_1' AND slugs.sequence = 1)) 
for - `user_2`
User.find("user_2")
SELECT sluggable_id FROM slugs WHERE ((slugs.sluggable_type = 'User' AND slugs.name = 'user_2' AND slugs.sequence = 1)) 
for - `user_3`
User.find("user_3")
SELECT sluggable_id FROM slugs WHERE ((slugs.sluggable_type = 'User' AND slugs.name = 'user_3' AND slugs.sequence = 1)) 

Its not taking sequence from the from the cached slug.

using - --(double dash)

for - `user--1`
User.find("user--1")
SELECT sluggable_id FROM slugs WHERE ((slugs.sluggable_type = 'User' AND slugs.name = 'user' AND slugs.sequence = 1)) 
for - `user--2`
User.find("user--2")
SELECT sluggable_id FROM slugs WHERE ((slugs.sluggable_type = 'User' AND slugs.name = 'user' AND slugs.sequence = 2)) 
for - `user--3`
User.find("user--3")
SELECT sluggable_id FROM slugs WHERE ((slugs.sluggable_type = 'User' AND slugs.name = 'user' AND slugs.sequence =3)) 

--(double dash) identifies the sequence properly

let me know if I am missing anything to include.

**Update - **

I debug gem code and found that it refers to sequence_separator as -- instead of which I have defined in model that is _

class String
  def parse_friendly_id(separator = nil)
    separator ||= FriendlyId::Configuration::DEFAULTS[:sequence_separator]
    name, sequence = split(/#{Regexp.escape(separator)}(\d+)?\z/)
    return name, (sequence ||= 1).to_i
  end
end
Sampat Badhe
  • 8,710
  • 7
  • 33
  • 49
  • I don't understand the issue? It is using the sequence seperator when creating a slug. For your "cached slugs" it then uses the slug name it created. So for `user_1` it looks for a slug with the name of `user_1` – j-dexx Feb 05 '16 at 13:33
  • Can you explain more what the "cached_slug" is? Those queries look like they're working fine. – Max Williams Feb 05 '16 at 13:33
  • @j-dexx I have updated my question. for `user_1` it should search slug name as `user` and and sequence number `1` and for `user_2` it should search slug name as `user` and and sequence number `2` and so on.. – Sampat Badhe Feb 07 '16 at 10:06
  • @MaxWilliams `cached_slug` column is use to store the latest slug. – Sampat Badhe Feb 07 '16 at 10:08

0 Answers0