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.
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:
FriendlyId version 5.4.1
Rails version 6.1.6.1
Ruby version 3.1.2
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.
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?
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:
We have the
friendly_id
scoped totype
so that bothVideo
andVideoExcerpt
can have the same slug. The STItype
in our application is thename
of the class (e.g.'Video'
,'VideoExcerpt'
)To explain the problem we are experiencing, let's assume we have a
Video
and aVideoExcerpt
both with the slug'awesome-video
'. When I try to find theVideo
(notVideoExcerpt
), I get aVideoExcerpt
.That's not what I am expecting. What seems to be happening is that since
VideoExcerpt
is a child ofVideo
and since we have two records in the same STI table with the slug'awesome-video'
theVideoExcerpt
is getting picked first in this case. However, our application is such that we never wantVideo.friendly.find('awesome-video')
i.e. searching on the parent model, to returnVideoExcerpt
, the subtype, especially since theslug
is scoped to thetype
.What are your thoughts on the best way of resolving this?