2
votes

I am building a Rails application and I am stuck when generating slug for an article with the second locale defined.
For the main locale (french), it checked if an article already have the title and if it’s the case add an integer (id) at the end but for the second locale (english) it’s just generate the slug without checking if article exists (which give me duplicated slugs).

Here is my model:

class Post < ActiveRecord::Base
  translates :title, :slug, :content, fallbacks_for_empty_translations: true 
  active_admin_translates :title, :slug, :content, fallbacks_for_empty_translations: true 

  extend FriendlyId 
  friendly_id :slug_candidates, use: [:slugged, :globalize, :finders]

  private

  def slug_candidates
    [:title, [:title, :deduced_id]] # works for main locale but not others
  end

  def deduced_id
    self.class.where(title: title).count + 1
  end
end

How can I get the id added to slug for secondary locales when article already exists with same title ?

Thanks for your help !

My Project:

  • Rails 4.2.6
  • ActiveAdmin 1.0.0.pre2
  • Globalize 5.0.1
  • FriendlyId 5.1.0
  • FriendlyId-globalize 1.0.0.alpha2
1

1 Answers

1
votes

I finally get it working updating the slug_candidates method like this:

def slug_candidates
  [[:title, :deduced_id]]
end