Skip to content

Instantly share code, notes, and snippets.

@kwhandy
Forked from matenia/post.rb
Created December 13, 2021 05:28
Show Gist options
  • Save kwhandy/1ac5022e06c4e8ed9979b281eee8a53b to your computer and use it in GitHub Desktop.
Save kwhandy/1ac5022e06c4e8ed9979b281eee8a53b 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