I issue queries like the following that retrieve exactly 0 or 1 records:
car = Car.where(vin: '1234567890abcdefg')
What's returned is of course a list of cars of length 1. So I end up adding .first
at the end of the query:
car = Car.where(vin: '1234567890abcdefg').first
Seems like there should be a better way in Rails. Does something like .onlyOne
exist which would return just a single Car
and throw an exception if there was more than 1?
car = Car.where(vin: '1234567890abcdefg').onlyOne
or
car = Car.onlyOne(vin: '1234567890abcdefg')