-
-
Save kwhandy/1ac5022e06c4e8ed9979b281eee8a53b to your computer and use it in GitHub Desktop.
Use custom method for FriendlyId (Example)
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 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