Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the unique generated title names of friendly-id

I am using the friendly_id gem. In the portfolio.rb I placed these two lines:

  extend FriendlyId
  friendly_id :title, use: :slugged

As you can see I am also using the slug option. When I create a project with title "example" it works find and I can find the project under mysite.com/projects/example. Now, if I create a second one with the same title I get a title for it like this one: mysite.com/projects/example-74b6c506-5c61-41a3-8b77-a261e3fab5d3. I don't really like this title. I was hoping for a friendlier title like example-2.

At this question, RSB (user) told me that its friendly_id that causes that. I was wondering if there is a way to create a more friendly. At first I thought of "manually" checking if the same title exists (in a while loop) and assigning another title using either example-2 or example-3 or... example-N.

However do I need to do something like that or am I missing something? Is there an easier way to do something like that?

like image 787
Stefanos.Ioannou Avatar asked Aug 19 '14 09:08

Stefanos.Ioannou


People also ask

Is the title field the same as the ID field?

Since the ID-field is always unique your title field will be too. Message 4of 4 1,958 Views 0 Kudos Reply Post Reply Helpful resources Announcements Register for a Free Workshop

How to add a title field to an item-ID?

What you could do is simply add the Title field as a hidden field in your Edit-form and set the value of the Item-ID as the text value of the Title field. Since the ID-field is always unique your title field will be too. Message 4of 4 1,958 Views

Do I need a patch to add a title field?

Report Inappropriate Content ‎03-27-201811:33 AM Patch is not necessarily needed. What you could do is simply add the Title field as a hidden field in your Edit-form and set the value of the Item-ID as the text value of the Title field. Since the ID-field is always unique your title field will be too.

How do I add a unique identifier to a querystring?

One of the commonest ways of sticking a unique identifier (UID) to a querystring in the .NET web world (or even classic ASP) is through Microsoft’s famous globally unique identifier, or guid. So you may see some kind of url like:


2 Answers

Check the documentation for the latest version of friendly_id:

A new "candidates" functionality which makes it easy to set up a list of alternate slugs that can be used to uniquely distinguish records, rather than appending a sequence.

Example straight from the docs:

class Restaurant < ActiveRecord::Base
  extend FriendlyId
  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],
      [:name, :street, :city],
      [:name, :street_number, :street, :city]
    ]
  end
end
like image 155
Thilo Avatar answered Sep 27 '22 22:09

Thilo


UUID

The problem you're alluding to is the way in which friendly-id appends a hash (they call it a UUID) to duplicate entries:

Now that candidates have been added, FriendlyId no longer uses a numeric sequence to differentiate conflicting slug, but rather a UUID (e.g. something like 2bc08962-b3dd-4f29-b2e6-244710c86106). This makes the codebase simpler and more reliable when running concurrently, at the expense of uglier ids being generated when there are conflicts.

I don't understand why they have done this, as it goes against the mantra of friendly ID, but nonetheless, you have to appreciate that's how it works. And whilst I don't think the slug_candidates method above will prove any more successful, I do think that you'll be able to use something like a custom method to define the slug you wish

--

You'll want to read this documentation (very informative)

It says there are two ways to determine the "slug" your records are assigned, either by using a custom method, or by overriding the normalize_friendly_id method. Here's my interpretation of both of these for you:

Custom Method

#app/models/project.rb
Class Project < ActiveRecord::Base
   extend FriendlyID
   friendly_id :custom_name, use: :slugged

   def custom_name
     name = self.count "name = #{name}"
     count = (name > 0) ? "-" + name : nil 
     "#{name}#{count}"
   end
end

Normalize_Friendly_ID

#app/models/project.rb
Class Project < ActiveRecord::Base
   extend FriendlyID
   friendly_id :name, use: :slugged

   def normalize_friendly_id
     count = self.count "name = #{name}"
     super + "-" + count if name > 0
   end
end
like image 44
Richard Peck Avatar answered Sep 27 '22 22:09

Richard Peck