norman / friendly_id

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.
http://norman.github.io/friendly_id/
MIT License
6.16k stars 592 forks source link

I do not understand why my `normalize_friendly_id` override is never called #981

Closed pil0u closed 3 years ago

pil0u commented 3 years ago

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 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?

pil0u commented 3 years ago

Moved my question to StackOverflow, sorry for the inconvenience.