Apparently FriendlyId has changed it's previously default method of appending a numeric sequence to duplicate slugs (which is what I want) to now use UUID:
Previous versions of FriendlyId appended a numeric sequence to make slugs unique, but this was removed to simplify using FriendlyId in concurrent code.
This functionality is not something I'm interested in at this time and would much prefer to have the original method that results in a cleaner URL. I found a similar question where someone provided the below code to override the friendlyId normalize_friendly_id
method to get to the functionality I'm after, but using it results in an error (wrong number of arguments (given 1, expected 0)
):
def normalize_friendly_id
count = self.count "name = #{name}"
super + "-" + count if name > 0
end
I attempted to "convert" this into a friendlyId "candidate" but I don't really know what I'm doing and the below doesn't work. Any thoughts on how I could tweak the name_candidate method to produce the result I'm afer?
class Folder < ApplicationRecord
extend FriendlyId
friendly_id :name_candidates, use: [ :slugged, :scoped ], scope: :account_id
has_ancestry
belongs_to :account
has_many :notes, dependent: :destroy
validates :name, presence: true
# # https://stackoverflow.com/a/25380607/523051
# # overrride friendlyId to append -number to duplicate folders instead of uuid's
# def normalize_friendly_id
# count = self.count "name = #{name}"
# super + "-" + count if name > 0
# end
def name_candidates
append_number = self.count "name = #{name}" if name > 0
[
:name,
:name, append_number
]
end
end
Note I am utilizing the :scoped
functionality of friendlyId, so checks for existing folder names should be correctly scoped to :account_id
.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…