active-hash / active_hash

A readonly ActiveRecord-esque base class that lets you use a hash, a Yaml file or a custom file as the datasource
MIT License
1.19k stars 178 forks source link

Fix nil scopes in method_missing and respond_to_missing? #278

Closed julianrubisch closed 11 months ago

julianrubisch commented 1 year ago

I ran into this error after I upgraded an app to Rails 7, which seems to have changed how association reflections are handled ?

Anyway, method_missing and respond_to_missing? were failing because of scopes being nil, which is always the case unless you specify at least one scope:

https://github.com/active-hash/active_hash/blob/7a07bcc07abca9aca48b47186c23f85cf6ec7ced/lib/active_hash/base.rb#L375-L376

I've fixed this using safe navigation. An alternative would be to declare a default for the class_attribute here: https://github.com/active-hash/active_hash/blob/7a07bcc07abca9aca48b47186c23f85cf6ec7ced/lib/active_hash/base.rb#L24-L25

Let me know what you prefer!

kbrock commented 11 months ago

Sorry. Not sure why this was not merged yet.