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.13k stars 589 forks source link

Slug regenerated #1010

Open jjf21 opened 1 year ago

jjf21 commented 1 year ago

Hi, we encounter a strange bug using the gem.

Some of our records have seen their slug generated 2 times, the first one correctly and the second one incorrectly.

>  FriendlyId::Slug.where(sluggable_type: "Contact", sluggable_id: contact.id)
=>
[#<FriendlyId::Slug:0x000055e8ed64a790
  id: 295947,
  slug: "tony-thee",
  sluggable_id: 17554,
  sluggable_type: "Contact",
  scope: nil,
  created_at: Sat, 06 May 2023 15:38:17.448619000 CEST +02:00>,
 #<FriendlyId::Slug:0x000055e8ed64a498
  id: 295948,
  slug: "36e81d8e-f5fd-4eaa-a3bd-4a77ab8db603",
  sluggable_id: 17554,
  sluggable_type: "Contact",
  scope: nil,
  created_at: Sat, 06 May 2023 15:38:17.655258000 CEST +02:00>]

We can see that the first one is created using the correct slug tony-thee and the second one is using a UUID, which is not the Contact#uuid.

the slug uses a full_name which is set in a before_validation callback

# models/contact.rb
friendly_id :full_name, use: :history
before_validation :set_full_name

We conclude that as the first slug created has always the good slug, so that it's not a problem with callbacks order.

This issue happened only on 60 records, out of 30k on production, but we can't find how to reproduce it local env.

I am looking for any know issues that could create this kind of behavior. Thanks for you help !