Skip to content

Instantly share code, notes, and snippets.

@matenia
Created May 10, 2012 01:29
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save matenia/2650300 to your computer and use it in GitHub Desktop.
Save matenia/2650300 to your computer and use it in GitHub Desktop.
Use custom method for FriendlyId (Example)
class Post < ActiveRecord::Base
# for SO: http://stackoverflow.com/questions/10481389/friendly-id-with-two-parameters
# EXAMPLE ASSUMES YOU ARE USING FRIENDLYID 4.X
# AND THAT YOU HAVE A SLUG:STRING COLUMN ON THE MODEL
# ...
extend FriendlyId
friendly_id :generate_custom_slug, :use => :slugged
# ...
# If the object is a new record, created_at and updated_at are not
# populated at the time of generating the slug, so default to Time.now
def generate_custom_slug
"#{:created_at || Time.now} #{:title}"
end
# you will need to run strftime and extract the required fields to get the proper date format
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment