Is is possible to force a Rails dynamic finder to throw an ActiveRecord::RecordNotFound
exception rather than return nil
when it cannot find a result?
For example, where a beverage of the name 'Nuka–Cola' does not exist:
@not_found = Beverage.find_by_name('Nuka–Cola')
Rather than having
@not_found == nil
Could the
.find_by_name('Nuka–Cola')
method call throw an ActiveRecord::RecordNotFound
exception?
Or am I going to have to check for nil
and throw the exception manually?
Use the bang version.
@not_found = Beverage.find_by_name!('Nuka–Cola')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With