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. Thanks, -mike --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
not sure if that would work in your case, but what about unique keys in your database? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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, etc). 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 Mueller <rails-mailing-l...@andreas- s.net> wrote:> not sure if that would work in your case, but what about unique keys in > your database? > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---