Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collisions when using finder with Rails STI subtypes #999

Open
gaganawhad opened this issue Oct 18, 2022 · 0 comments
Open

Collisions when using finder with Rails STI subtypes #999

gaganawhad opened this issue Oct 18, 2022 · 0 comments
Labels

Comments

@gaganawhad
Copy link
Contributor

gaganawhad commented Oct 18, 2022

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.

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?

@parndt parndt added the pinned label Nov 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants