Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

friendly_id generate slug with ID

I am trying to use the friendly_id gem to generate a slug in the format of "#{id}-#{title}"

It looks like friendly_id uses before_save, and won't have access to the ID attribute.

Is there a work around for this ?

# Permalinks
#-----------------------------------------------------------------------------
extend FriendlyId
friendly_id :id_and_title, :use => :slugged

def id_and_title
  "#{id} #{title}"
end
like image 849
Andrew Cetinic Avatar asked Feb 06 '12 23:02

Andrew Cetinic


1 Answers

Instead of using friendly_id for this, you could override to_param in your model, to include the title

class YourModel < ActiveRecord::Base
  def to_param
    "#{id} #{title}".parameterize
  end
end

This should have the effect you're after without having to use friendly_id.

like image 78
alol Avatar answered Sep 21 '22 00:09

alol