# Is validates\_uniqueness\_of transaction safe?

**URL:** <https://www.ruby-forum.com/t/is-validates-uniqueness-of-transaction-safe/111913>\
**Category:** Rails\
**Created:** [August 13, 2007, 12:17pm UTC](https://www.ruby-forum.com/t/is-validates-uniqueness-of-transaction-safe/111913 "2007-08-13T12:17:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dparkmit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dparkmit](https://www.ruby-forum.com/u/dparkmit)\
**Post date:** [August 13, 2007, 12:17pm UTC](https://www.ruby-forum.com/t/is-validates-uniqueness-of-transaction-safe/111913/1 "2007-08-13T12:17:31Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![dparkmit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dparkmit](https://www.ruby-forum.com/u/dparkmit)\
**Post date:** [August 14, 2007, 1:49pm UTC](https://www.ruby-forum.com/t/is-validates-uniqueness-of-transaction-safe/111913/2 "2007-08-14T13:49:49Z")

</div>

dparkmit wrote:

> 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.
