Pergunta

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!

Foi útil?

Solução

Maybe something like this?

Class Market < ActiveRecord::Base
  has_friendly_id :friendly_name

  def friendly_name
    self.display_name || self.name
  end
end
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top