-
-
Notifications
You must be signed in to change notification settings - Fork 594
enhancement: array finders #1035
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
base: master
Are you sure you want to change the base?
Conversation
Hi @norman, thank you for the amazing work on this gem! I would appreciate a review on this PR. Could you please let me know if there is a chance for this to get merged, or if I should switch to a forked solution instead? |
Hello! Thanks for taking the time to submit such a well-thought out pull request to this library. I haven't been involved with the project for quite a few years, @parndt has done almost all maintaining for a long time. I won't offer an opinion of whether this should be merged, but I will offer some context. FriendlyId used to have this feature and I removed it (I think in version 4) because it was was hard to maintain:
When I removed this feature, very few people complained, so I felt that I had probably been spending a lot of time maintaining a complicated feature that few people were using, and I never looked back. My thoughts at the time were that if you really needed to look up multiple slugs in a high performance way, that was an unusual enough case for this library that we maintainers could reasonably expect that you should write your own custom query against the slug column like:
or do something similar against the slugs table if you're using History. If you feel strongly that you want this released soon, you might want to consider writing it as a plugin or an add-on to FriendyId in a separate gem, that would probably make it easier for you to get people to use it and wouldn't impose the same maintainence burden for you that a full-on fork would entail. FriendlyId was designed to be extensible from the beginning, with most of its core functionality implemented as add-ons. |
Hey @norman. I am Adrian, the author of Avo and a teammate of @Paul-Bob. Let me give you some context into why we would love to have this change and why we probably can't do what you suggest very easily. Avo is a bring-your-own library kind of situation. We don't have Regarding the proposed change (, it's again something out of our control. # we tell them to add this bit of code in resources which use friendly_id
# we'd rather have it "just work"
self.find_record_method = -> {
if id.is_a?(Array)
Post.where(slug: id)
else
Post.find(id)
end
} So yeah, this would improve compatibility with existing libraries which use the Personally, I'd love to be able to do things even if they are less performant than to have to jump through hoops to implement something to patch the original behavior. Thanks for listening, and we are open to other suggestions as well. |
Fixes #1034
Possibly fixes #1018 (not confirmed)
Description
This PR adds support for finding records using an array of slugs, similar to how ActiveRecord handles arrays of numeric IDs. If FriendlyId is the "Swiss Army bulldozer" of slugging plugins, this enhancement adds laser-guided precision to that bulldozer!
Problem
Previously, while single slug lookups worked fine:
Array slug lookups would fail:
Solution
Now you can find records using:
GlossaryTerm.find(["one", "two"])
GlossaryTerm.find(["one", 2, "three"])
(slugs + numeric IDs)GlossaryTerm.find(["one", "missing"], allow_nil: true)
GlossaryTerm.active.find(["one", "two"])
Key Features
ActiveRecord::RecordNotFound
allow_nil: true
for partial resultsTesting
Added comprehensive test coverage including:
This enhancement makes FriendlyId even more developer-friendly while maintaining it's robustness.