Change the unique generated title names of friendly-id using attribute of another table

293 views Asked by At

I have a Company Model, and i am using friendly_id like this friendly_id :name, use: :slugged

But since there can be many Company with the same name (different branches). I am trying to handle that case by using city attribute from the address of the Company.

But the Company address is stored in a different table Address.

so company.address.city gives me the city of the company.

friendly_id :slug_candidates, use: :slugged

  # Try building a slug based on the following fields in
  # increasing order of specificity.
  def slug_candidates
    [
      :name,
      [:name, :city]
    ]
  end

I'm aware i can do something like above. But since city is not an attribute of company how can i achieve this?

Update: Possible solution for this is to create a helper method city which returns the company's city.

But the problem never was that.

The version of friendly_id i am using is 4.0.10.1 and the feature which enables the usage of slug_candidates are available in versions 5 and above.

I tried updating the gem. But it wont get updated as version 5 has dependency on activerecord 4.0 and rails has dependency on activerecord 3.2.13

It's kind of a deadlock. Don't know what to do

1

There are 1 answers

4
Viktor Leonets On BEST ANSWER
class Company < ActiveRecord::Base

  .............................
  def city
    self.address.city
  end
end