views:

174

answers:

2

Can Permalink_fu combine 2 or more model attributes to create a unique permalink?

Let's say I have a Business Model, this model contains :name, :address, :phone, :city, :state, :country etc. attributes.

Right now I have permalink set up in this model only for :name

has_permalink :name

So I would get "/biz/name". However I would like to combine the Business name, city, and a incremental number if there are more than 1 location in the city for that business.

For example I would like to use:

"/biz/joes-coffee-shack-chicago" for the permalink

or if a multple location business

"/biz/starbucks-chicago-92"

Is this possible with the current permalink_fu plugin or some fork of permalink_fu? Or will this require some modification to the permalink_fu plugin?

A: 

Add a virtual attribute to your Business model.

class Business < ActiveRecord::Base
  attr_accessor :perma_link_attr
  has_permalink :perma_link_attr

  def perma_link_attr
    suffix = 1
    [:name, :city, suffix].join("-")
  end

end
KandadaBoggu
Thanks for your solution. Thomas is right that permalink_fu has it baked in already but I didn't realize until he pointed it out.
Ranknoodle
+1  A: 

You can set the attributes as an array:

has_permalink [:one, :two, :three]

They will be automatically joined by -. Permalink_fu also automatically adds a suffix if there's already a record with that permalink.

Tomas Markauskas
Thanks this is exactly what I was looking for!
Ranknoodle