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

Collisions when using finder with Rails STI subtypes #999

Open gaganawhad opened 2 years ago

gaganawhad commented 2 years ago

Firstly, thanks for your work on FriendlyId and for maintaining it. I appreciate it!

Here is an issue we are experiencing using FriendlyId with Rails STI and would love to hear if you have thoughts on it. We are using:

Our models are defined as so, as an example:

class Video < ActiveRecord::Base
  friendly_id :title_for_friendly_id, use: :scoped, scope: :type

end

class VideoExcerpt < Video
end

We have the friendly_id scoped to type so that both Video and VideoExcerpt can have the same slug. The STI type in our application is the name of the class (e.g. 'Video', 'VideoExcerpt')

To explain the problem we are experiencing, let's assume we have a Video and a VideoExcerpt both with the slug 'awesome-video'. When I try to find the Video (not VideoExcerpt), I get a VideoExcerpt.

Video.friendly.find('dont-waste-your-life').type
=> "VideoExcerpt"
Video.friendly.find('dont-waste-your-life').class
=> VideoExcerpt

That's not what I am expecting. What seems to be happening is that since VideoExcerpt is a child of Video and since we have two records in the same STI table with the slug 'awesome-video' the VideoExcerpt is getting picked first in this case. However, our application is such that we never want Video.friendly.find('awesome-video') i.e. searching on the parent model, to return VideoExcerpt, the subtype, especially since the slug is scoped to the type.

What are your thoughts on the best way of resolving this?