0

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

Alfie
  • 2,706
  • 1
  • 14
  • 28

1 Answers1

1
class Company < ActiveRecord::Base

  .............................
  def city
    self.address.city
  end
end
Viktor Leonets
  • 473
  • 4
  • 13
  • I'm sure this should work. But i'm facing another issue now. and did a little reading and the reason behind it is because version of friendly_id is not v5 or higher. – Alfie Jun 12 '15 at 20:14
  • And let me add i can't upgrade friendly_id as i am getting an error, – Alfie Jun 12 '15 at 20:15
  • ```In Gemfile: rails (= 3.2.13) ruby depends on activerecord (= 3.2.13) ruby friendly_id (~> 5.1.0) ruby depends on activerecord (>= 4.0.0) ruby ``` – Alfie Jun 12 '15 at 20:15
  • I tried this with another application running on rails 4.0 and it worked – Alfie Jun 13 '15 at 11:53