[Boulder Ruby Group] ar validations licking the balls
Charlie Savage
cfis at savagexi.com
Sun Nov 11 14:49:16 EST 2007
Peter Williams wrote:
> Tony Arcieri wrote:
>> If you could get a table level lock, then checking the uniqueness of a
>> value before performing an insert/update could be an atomic, as well as
>> performing a find_or_create_by type operation
>
> This approach would work but I have a hard time imagining wanting to
> take the performance hit of a table lock on any operation that happens
> often enough to make this race condition a real issue.
>
> A workable approach for the case of find_or_create is to enforce the
> uniqueness with a database constraint and do this in ruby code:
>
> 1. do a find
> 2a. return the model object if you found it
> 2b. if it does not exist try to insert it
> 3a. if the insert succeeds return the new object
> 3b. if the insert fails re-run the find and return the result of
> that.
Mostly the same idea (also does update), but hides the nastiness in a
postgresql procedure:
http://www.varlena.com/GeneralBits/126.php
And I don't think it requires a savepoint.
Charlie
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3237 bytes
Desc: S/MIME Cryptographic Signature
Url : http://rubyforge.org/pipermail/bdrg-members/attachments/20071111/6f79bda5/attachment.bin
More information about the Bdrg-members
mailing list