Created
November 22, 2011 20:28
-
-
Save etagwerker/1386833 to your computer and use it in GitHub Desktop.
Trying to use friendly_id with acts_as_nested_set
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
generatein a method in your model, and then doingfriendly_id :your_method.