How to handle this AR exception in rails?

I’m trying to handle some invalid active record syntax. Not sure how to go about it?

ActiveRecord::StatementInvalid at /receiving_queries/2


It’s not handled by the standard error? (or default).

Thanks, Joe

Like any other Rails exceptions, you have two options (the second is preferred):

x = Model.where(...) rescue nil
if x.nil?
    <handle error here>


    x = Model.where(...)
rescue StandardError   # or whatever exception is being thrown by PG
    <handle error here>

Hi, thanks. I used the second method to trap standard errors. But other errors crashed my application. I guess i need to trap more errors?

Yes, there will almost always be a need to trap more different errors. I have otherwise stable code that has been in use for over 10 years that still requires modified error handling from time to time. I generally make sure that all errors are trapped, but different errors will require different handling; some are recoverable and others are not.