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

Use of STI with scoped can cause "duplicate entry" errors #744

Closed
mpasad opened this issue Apr 27, 2016 · 2 comments
Closed

Use of STI with scoped can cause "duplicate entry" errors #744

mpasad opened this issue Apr 27, 2016 · 2 comments
Labels

Comments

@mpasad
Copy link

mpasad commented Apr 27, 2016

I am using Rails 4.1.14.2 and FriendlyId 5.0.3

I have a Project class that has multiple Topics. Topic slugs need to be unique across all topics within the same project.

My routes are like localhost/:project/sub-topic-slug

Topics class uses friendlyId and an STI table table has SubTopics
class Topic
extend FriendlyId
friendlyId :slug_candidates, use: :scoped, scope: :project

SubTopic1 < Topic

FriendlyId is allowing slugs to be same for the SubTopic1 and SubTopic2. When I look at the SQL generated for scope it is automatically adding type column from the STI. But the documentation says that slugs will not be unique per subclass by default. How can I change this behavior?

Looking at the source code the method "scope_for_slug_generator":

in slugged.rb uses: scope = self.class.base_class.unscoped

but in scoped.rb uses: relation = self.class.unscoped.friendly

Changing this line to self.class.base_class.unscoped.friendly fixes the problem.
Thanks.

@mpasad mpasad changed the title Use of STI can cause "duplicate entry" errors Use of STI with scoped can cause "duplicate entry" errors Apr 27, 2016
@mpasad
Copy link
Author

mpasad commented Apr 27, 2016

#745

@stale
Copy link

stale bot commented Dec 11, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Dec 11, 2019
@stale stale bot closed this as completed Dec 18, 2019
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

1 participant