Rails validation is inefficient

James B. wrote:

I digress for a moment, but it seems to me that the proper way to handle
this in Rails would have been to INSERT first, trap any error and THEN
do a find on the index to see if the error was caused by a duplicate.
If a duplicate is found and is different (based on hash contents of
inserted row less id attribute versus hash of returned row lest id
attribute?) than the row just inserted then report a validation error;
if not then raise the original dbms error. That way the race condition
would never occur and there would, on average, be
((1 / ) - 1) fewer SQL reads.

Unstated above, but necessary and implied, is that uniqueness
constraints are and must be enforced at the DBMS level. Rails simply
checks for conformance.