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

Add allow_nil: true option to the finder #995

Merged
merged 8 commits into from
Jul 14, 2022

Conversation

joemsak
Copy link
Contributor

@joemsak joemsak commented Jul 4, 2022

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

MyModel.friendly.find("friendly-slug")
# => still works as expected, raises an ActiveRecord::RecordNotFound exception

MyModel.friendly.find("friendly-slug", allow_nil: true)
# => returns nil instead

@joemsak joemsak changed the title Add raise: false or allow_nil: true option to the finder Add allow_nil: true option to the finder Jul 14, 2022
@parndt parndt changed the title Add allow_nil: true option to the finder Add allow_nil: true option to the finder Jul 14, 2022
README.md Outdated Show resolved Hide resolved
Co-authored-by: Philip Arndt <git@p.arndt.io>
@parndt parndt merged commit 9f94bba into norman:master Jul 14, 2022
@parndt
Copy link
Collaborator

parndt commented Jul 14, 2022

Thanks, Joe! 🎉

parndt added a commit that referenced this pull request Jul 21, 2022
Turns out, the documentation I added in #995 wasn't 100% accurate. I had
to add tests for such examples and improve the internal code to support
the option. This should now provide the fully expected behavior.

Co-authored-by: Philip Arndt <git@p.arndt.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants