Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-3.2friendly-id

Friendly id in nested model


I'm using friendly_id to generate URLs on my rails application.

I have to following model organization :

Ad :

class Ad < ActiveRecord::Base
 belongs_to :area
 ...

Area :

class Area < ActiveRecord::Base
 has_many :ads
 validates :name
 ...

And the following function in Ad model to generate the URL for each Ad :

friendly_id :title_and_title, use: :slugged 

def title_and_title
 "classified ads france #{title}"
end

What I'd like to do is to add the area name in the URL. How could I do that ?

Thanks


Solution

  • Try:

    def title_and_title
      self.area.name
    end
    

    To get the name for it's parent.