Avoiding duplicate inserts?

I’m wondering if there is a good Rails solution to avoiding duplicate
inserts with identical content. I know of the locking to prevent
updates, but looking for something on inserts.

For example, say you have an API to a blog system that lets a caller
post a comment. The client calls “addComment” with the same parameters
(say, user_id and comment) twice and each request is handled by a
different mongrel instance. How can the duplicate comment be prevented
from being saved? They would be different rows, and I would like to
avoid table locking. (In this example, the client can’t be controlled,
so it needs to be server-side.)

I believe that a validating on before_save is not enough because there
would still be a window between the validation completing and writing
the row since it is only the row that is locked.

Any suggestions would be appreciated.



not sure if that would work in your case, but what about unique keys in
your database?

Sounds like a job for validates_uniqueness_of :comment, :scope=>:user

The double-submit issue is a matter to solve on the client (e.g.,
disable the submit button after it’s clicked; clear the comment field,

For now, I’d recommend that you take this simple approach and let the
real users’ use demonstrate that more needs to be done. I suspect
you’re concern (beyond this) is more theoretical than practical.

On Apr 25, 4:59 am, Thorsten M. <rails-mailing-l…@andreas-