Change Unique Names of Friendly ID Names
I am using the friendly_id gem. In the .rb portfolio, I posted these two lines:
extend FriendlyId
friendly_id :title, use: :slugged
As you can see, I am using the slug parameter as well. When I create a project called "example" it works and I can find the project under mysite.com/projects/example
. Now, if I create a second one with the same name, I get a title for it, like this one: mysite.com/projects/example-74b6c506-5c61-41a3-8b77-a261e3fab5d3
. I don't like this title. I was hoping for a friendlier title, for example example-2
.
In this question, RSB (the user) told me that his friendly_id is causing this. I was wondering if there is a way to create a friendlier one. At first I thought about doing "manually" checking if the same title exists (in a while loop) and assigning a different title using example-2 or example-3 or ... example-N.
However, do I need to do something, or am I missing something? Is there an easier way to do something like this?
source to share
Check the documentation for the latest friendly_id:
New "Candidates" feature that makes it easier to customize a list of alternative slugs that can be used to uniquely recognize records rather than add a sequence.
An 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
source to share
UUID
The problem you are talking about is how it adds (they call UUIDs) duplicate entries: friendly-id
hash
Now that candidates have been added, FriendlyId no longer uses a number sequence to differentiate a conflicting slug, but rather a UUID (for example, something like 2bc08962-b3dd-4f29-b2e6-244710c86106). This makes the codebase simpler and more reliable when running concurrently by introducing weaker identifiers when conflicts occur.
I don't understand why they did it as it goes against the mantra of friendly ID, but you should still appreciate how it works. And while I don't think slug_candidates
the above method will prove to be more successful, I think you can use something like custom method
to determine the slug you want
-
You want to read this documentation (very informative)
It says that there are two ways to define the "bullet" that your entries assign, either through a custom method or by overriding the method normalize_friendly_id
. Here's my interpretation of both of them 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
source to share