I am trying to find a model by an array of ids like so:
Model.find [1,2,3]
but let's say only two of these models exist (the model with id of 2 was deleted):
#<Model id: 1>
#<Model id: 3>
I get an error like this:
#<ActiveRecord::RecordNotFound: Couldn't find all Models with 'id': (1, 2, 3) (found 2 results, but was looking for 3).>
Is it possible to catch this error and determine which of the models were not present?
say i'm in my controller:
def index
@models = Model.find params.require(:model_ids)
rescue ActiveRecord::RecordNotFound => e
e.full_message
???
end
I'd like to, in the ??? line, run some code on the exception e that will return 2, letting me know what of the models was not found, so I can re-run the query without it while noting which ones weren't found.
whereand then check the difference by comparing the resulting ids with the given ones?