views:

24

answers:

1

I'm using the friendly_id plugin to generate SEO-friendly URLS for some of my models.

Currently, I have a model with two attributes: name and display_name.

Essentially, display_name is preferred, but if it is blank, the model reverts to name. Friendly_id needs a field to base the URL off of:

Class Market < ActiveRecord::Base
  has_friendly_id :name
end

How can I implement something that looks (logically) like this:

Class Market < ActiveRecord::Base
  if self.display_name
    has_friendly_id :display_name
  else
    has_friendly_id :name
  end
end

Thanks!

+2  A: 

Maybe something like this?

Class Market < ActiveRecord::Base
  has_friendly_id :friendly_name

  def friendly_name
    self.display_name || self.name
  end
end
Tony Fontenot
Beat me to it by milliseconds. Exactly the same answer! +1 More details here : http://norman.github.com/friendly_id/file.Guide.html#using_a_custom_method_to_generate_the_slug_text
Shadwell