Search code examples
ruby-on-railsfriendly-id

Is there way to use friendly_id for not unique value


I'm writing a rails project that is about stock market. There is a ticker symbol that represent a company. e.g. "AAPL" for Apple.

But the ticker symbol is only unique for current listed companies, the symbols are duplicated when delisted companies are included. And I want to include also delisted companies to the system.

Currently I'm thinking to create two columns for ticker symbols like ticker and ticker_for_friendly_id. And only currently existing companies have both values and delisted companies only have ticker values.

But to save same value in two columns are bit redundant, is there a better way to implement it situation like this?


Solution

  • friendly_id :slug_candidates, use: :slugged
    
    def slug_candidates
      [
        :ticker_slug
      ]
    end
    
    def ticker_slug
      str = ticker
      str += '-unlisted' if unlisted?
    end