Skip to content

Instantly share code, notes, and snippets.

@etagwerker
Created November 22, 2011 20:28
Show Gist options
  • Select an option

  • Save etagwerker/1386833 to your computer and use it in GitHub Desktop.

Select an option

Save etagwerker/1386833 to your computer and use it in GitHub Desktop.
Trying to use friendly_id with acts_as_nested_set
class Category < ActiveRecord::Base
include Liquid::Category
extend FriendlyId
friendly_id :name, :use => :slugged, :slug_column => 'permalink',:slug_generator_class => CategorySlugGenerator
acts_as_nested_set :dependent => :destroy
...
end
# Then
# Knows how to generate a slug for a category
class CategorySlugGenerator < FriendlyId::SlugGenerator
def generate
if self.sluggable.parent_id.nil?
result = self.sluggable.name.to_url + "/"
else
result = self.sluggable.parent.permalink + (self.sluggable.permalink.blank? ? self.sluggable.name.to_url : self.sluggable.permalink.split("/").last) + "/"
end
result
end
end
@norman
Copy link

norman commented Nov 22, 2011

I don't know the whole context here, but I suspect you do not need to override the slug generator class for this, you should be able to get away with just defining generate in a method in your model, and then doing friendly_id :your_method.

@etagwerker
Copy link
Author

@norman, thanks, i assume that will take care of conflicts, right?

@norman
Copy link

norman commented Nov 22, 2011

Yes, exactly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment