8000 fix: add guard clause to check if matches is nil by ratheeshaditya · Pull Request #917 · countries/countries · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: add guard clause to check if matches is nil #917

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

Merged
merged 2 commits into from
Jun 30, 2025

Conversation

ratheeshaditya
Copy link
Contributor
@ratheeshaditya ratheeshaditya commented Jun 5, 2025

Hi,

After the release of Country version 8, certain parts of our application began breaking with the following error:

   NoMethodError:
     undefined method `[]' for nil
   # ./lib/countries/country/finder_methods.rb:49:in `respond_to_missing?'

We traced the issue to the matches variable being nil and still being accessed with an index in finder_methods. To reproduce this, I’ve added a spec that demonstrates the failure.

This PR fixes the issue by adding a guard clause to return early, if matches is nil preventing the NoMethodError.

Our Usage


The behavior surfaced when using the gem in combination with the CanCanCan authorization gem. Internally, CanCanCan calls respond_to?(:arr) on the ISO3166::Country object. That call triggered the error shown above.

If this fix isn’t appropriate or there's a preferred way of doing it, kindly comment :)

@ratheeshaditya ratheeshaditya changed the title fix: check if matches is nil after breaking changes fix: add gaurd clause to check if matches is nil Jun 6, 2025
@pmor
Copy link
Member
pmor commented Jun 6, 2025

Thanks for the PR!

@pmor pmor changed the title fix: add gaurd clause to check if matches is nil fix: add guard clause to check if matches is nil Jun 6, 2025
@ratheeshaditya
Copy link
Contributor Author
ratheeshaditya commented Jun 7, 2025

EDIT: I've added an additional condition to ensure the cases are covered

@ratheeshaditya ratheeshaditya force-pushed the fix_not_null_match branch 2 times, most recently from 5c8ce0b to 9866d17 Compare June 12, 2025 07:04
- This change guarantees the return of a boolean
@pmor pmor merged commit 65f40e4 into countries:master Jun 30, 2025
10 of 11 checks passed
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
0