-
-
Notifications
You must be signed in to change notification settings - Fork 593
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
Can we have a find_by, returning nil when not found #937
Comments
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. |
@fidalgo does |
@parndt I'm using |
Does the below work, without the friendly filter applied? Model.find_by(slug: params[:id]) My reasoning is that |
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. |
I'm sorry, @parndt but I guess we're having a communication issue here. With friendly_id gem, I can use the method So my request is to add another method that will mimic the so instead of raising an exception is will return When I run Please let me know if I'm missing something! |
@fidalgo understood, I think. I'm mostly wondering why we need to add that when Rails does it already? 😄 it seems like FriendlyId doesn't need a method for doing what Rails does already. That is why we put our functionality behind |
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. |
I found this issue (sup, @parndt how you been) looking for something similar: For me the idea would be that you don't know if you have primary key ID or slug friendly ID and still want a way to use them interchangeably and just get a nil instead of a raised error: MyModel.friendly.find_safe(id_or_slug)
# not saying the method has to be called `find_safe()` but hopefully you get the idea Does that make sense? |
@joemsak I think the idea does make sense in principle, and we could probably support it using something like Then this implementation friendly_id/lib/friendly_id/finder_methods.rb Lines 16 to 23 in a33d246
raise_not_found_exception(id) if raise .
We would need to formalise the args though away from Super nice to hear from you too, Joe! ❤️ |
Yeah, to be sure, my present needs for it almost certainly result from deeper code design issues where the |
BTW @parndt if you are interested in this idea, would you like me to try a go at a PR? |
yes please, I think it'd be good to see if it's feasible, and the idea makes sense. 😄 |
This PR adds an optional `allow_nil: true` option to the finder From discussion in #937 the idea is to add functionality that behaves *like* Rails' `find_by` which returns `nil` when a record is not found. This is useful in conditions where the developer is allowing the primary key ID and the friendly slug ID but is not sure if the record will actually be found, and wants a nil instead of a raised exception. ### Usage ```ruby MyModel.friendly.find("friendly-slug") # => still works as expected, raises an ActiveRecord::RecordNotFound exception MyModel.friendly.find("friendly-slug", allow_nil: true) # => same as above, but some devs may find using this option to be more self-documenting of their intentions ```
Instead of having a
find
that will throw an exception, it would help to have afind_by
method, that would have the same behaviour asfind_by
in rails, where it returns the first match or nil?The text was updated successfully, but these errors were encountered: