# Validate: Only one row set to boolean at any time

**URL:** <https://www.ruby-forum.com/t/validate-only-one-row-set-to-boolean-at-any-time/71678>\
**Category:** Rails\
**Created:** [October 10, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/validate-only-one-row-set-to-boolean-at-any-time/71678 "2006-10-10T18:57:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RoR\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@RoR\_newbie](https://www.ruby-forum.com/u/RoR_newbie)\
**Post date:** [October 10, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/validate-only-one-row-set-to-boolean-at-any-time/71678/1 "2006-10-10T18:57:12Z")

</div>

How can I validate that only one of the rows in my database is true and  
rest are false at any give time?

I’ve already figured out to do this except if I try to update the row  
that is true. Then it fails.

Here’s the code: [http://rafb.net/paste/results/Ryjnbq78.html](http://rafb.net/paste/results/Ryjnbq78.html)

What needs to be done to make it work?

---

<div class="post-metadata">

**Author:** ![RoR\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@RoR\_newbie](https://www.ruby-forum.com/u/RoR_newbie)\
**Post date:** [October 10, 2006, 6:58pm UTC](https://www.ruby-forum.com/t/validate-only-one-row-set-to-boolean-at-any-time/71678/2 "2006-10-10T18:58:07Z")

</div>

Uh, messed the subject line. It should say “true” instead of “boolean”  
of course.

---

<div class="post-metadata">

**Author:** ![RoR\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@RoR\_newbie](https://www.ruby-forum.com/u/RoR_newbie)\
**Post date:** [October 10, 2006, 10:23pm UTC](https://www.ruby-forum.com/t/validate-only-one-row-set-to-boolean-at-any-time/71678/3 "2006-10-10T22:23:36Z")

</div>

Another way I figured is:

validates\_uniqueness\_of :is\_visitor

And then add a check: If is\_visitor is true, validate, if it’s false,  
then don’t. That would work perfectly but I don’t know how to access the  
data being submitted for validation.

Ideas on how to do that?

---

<div class="post-metadata">

**Author:** ![RoR\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@RoR\_newbie](https://www.ruby-forum.com/u/RoR_newbie)\
**Post date:** [October 11, 2006, 10:11am UTC](https://www.ruby-forum.com/t/validate-only-one-row-set-to-boolean-at-any-time/71678/4 "2006-10-11T10:11:40Z")

</div>

Anyone have any ideas? I’m pretty stuck with this now.
