문제

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!

도움이 되었습니까?

해결책

Maybe something like this?

Class Market < ActiveRecord::Base
  has_friendly_id :friendly_name

  def friendly_name
    self.display_name || self.name
  end
end
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top