Hi All, I''ve recently starting programming in RoR. Here''s a question re: the ActiveRecord validation methods. Are they transaction safe? It seems that saving an instance of a model with validation methods could require multiple db queries. For example, validated_uniqueness_of would need to do at least one query to figure out if the value already exists and then the second query to insert a new record. Are these queries contained in a single transaction? If not, how do you make sure that they are part of the same transaction? Thanks for your help! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Mark Reginald James
2007-Aug-14 11:47 UTC
Re: Is validates_uniqueness_of transaction safe?
dparkmit wrote:> I''ve recently starting programming in RoR. Here''s a question re: the > ActiveRecord validation methods. Are they transaction safe? > > It seems that saving an instance of a model with validation methods > could require multiple db queries. For example, > validated_uniqueness_of would need to do at least one query to figure > out if the value already exists and then the second query to insert a > new record. > > Are these queries contained in a single transaction? If not, how do > you make sure that they are part of the same transaction?Yes validates_uniqueness_of may fail when there concurrent validations because it doesn''t lock the table. To be sure you''d have to either lock the table in a before_validation callback or add a unique index to the table and handle the error. -- We develop, watch us RoR, in numbers too big to ignore. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---