# Quick validation question

**URL:** <https://www.ruby-forum.com/t/quick-validation-question/157604>\
**Category:** Rails\
**Created:** [January 13, 2009, 5:13pm UTC](https://www.ruby-forum.com/t/quick-validation-question/157604 "2009-01-13T17:13:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![drsharp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@drsharp](https://www.ruby-forum.com/u/drsharp)\
**Post date:** [January 13, 2009, 5:13pm UTC](https://www.ruby-forum.com/t/quick-validation-question/157604/1 "2009-01-13T17:13:18Z")

</div>

Hi, I have a model which contains a boolean value. I would like to set  
a validation which only allows this value to be set to true for one  
record at a time in the table. Is it possible to do this with  
validates\_uniqueness? or is there another way?

Any help would be appreciated, thanks.

---

<div class="post-metadata">

**Author:** ![drsharp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@drsharp](https://www.ruby-forum.com/u/drsharp)\
**Post date:** [January 13, 2009, 6:13pm UTC](https://www.ruby-forum.com/t/quick-validation-question/157604/2 "2009-01-13T18:13:24Z")

</div>

Is it possible to do this with

> validates\_uniqueness?

validates\_uniqueness on a boolean would be great if you only ever want 2  
records in your table…

Perhaps doing a quick count of records with the value set before saving  
your current create/update would achieve what you want.

---

<div class="post-metadata">

**Author:** ![drsharp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@drsharp](https://www.ruby-forum.com/u/drsharp)\
**Post date:** [January 13, 2009, 6:25pm UTC](https://www.ruby-forum.com/t/quick-validation-question/157604/3 "2009-01-13T18:25:11Z")

</div>

Dan S. wrote:

> Hi, I have a model which contains a boolean value. I would like to set  
> a validation which only allows this value to be set to true for one  
> record at a time in the table. Is it possible to do this with  
> validates\_uniqueness? or is there another way?

validates\_uniqueness\_of :is\_root, :if =\> :is\_root should work.

–  
Rails Wheels - Find Plugins, List & Sell Plugins -  
[http://railswheels.com](http://railswheels.com)
