FriendlyId is the “Swiss Army bulldozer” of slugging and permalink plugins for ActiveRecord. It allows you to create pretty URL’s and work with human-friendly strings as if they were numeric ids for ActiveRecord models.
I am very new to friendly_id and it matches my need to provide friendly URLs :+1:
I have a Group model (i.e. a group of users) for which I generate a unique code upon creation. FYI, this code attribute is set as a unique index in my PostgreSQL database.
class Group < ApplicationRecord
include FriendlyId
friendly_id :code
after_create :generate_group_code
private
def normalize_friendly_id(value)
super.upcase
end
def generate_group_code(size = 8)
allowed_chars = ("a".."z").to_a
code = (1..size).map { allowed_chars[rand(26)] }.join while Group.exists?(code: code)
update(code: code)
end
end
I think I have followed the gem's guide properly, I just want the generated code to be upcased in the URLs (i.e. /group/ABCDEFGH).
The friendly_id is indeed set as my code attribute, but it is not upcased. I placed a byebug in the normalize_friendly_id method but it is never triggered. What am I missing?
Hello,
I am very new to
friendly_id
and it matches my need to provide friendly URLs :+1:I have a
Group
model (i.e. a group of users) for which I generate a uniquecode
upon creation. FYI, thiscode
attribute is set as a unique index in my PostgreSQL database.I think I have followed the gem's guide properly, I just want the generated
code
to be upcased in the URLs (i.e./group/ABCDEFGH
).The friendly_id is indeed set as my
code
attribute, but it is not upcased. I placed abyebug
in thenormalize_friendly_id
method but it is never triggered. What am I missing?