# Validating Uniqueness From Two Database Tables?

**URL:** <https://www.ruby-forum.com/t/validating-uniqueness-from-two-database-tables/137167>\
**Category:** Rails\
**Created:** [May 9, 2008, 4:49pm UTC](https://www.ruby-forum.com/t/validating-uniqueness-from-two-database-tables/137167 "2008-05-09T16:49:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lottario](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de053/32.png) [@lottario](https://www.ruby-forum.com/u/lottario)\
**Post date:** [May 9, 2008, 4:49pm UTC](https://www.ruby-forum.com/t/validating-uniqueness-from-two-database-tables/137167/1 "2008-05-09T16:49:36Z")

</div>

Well, I’m working on adding a new type of user (organization) to my  
application, using a different database table than the standard User  
table (in the end, I think it works out being easier to deal with than  
creating another column in the original User table for account type).  
However, I’m running into a user registration problem. I can register  
both types of users fine, however, a screen name can be shared between  
the different tables. For example, a user can register with the screen  
name “foo”, and an organization can also register with the screen name  
“foo”. Obviously, this creates login problems, so I’d like to try to  
prevent this. Unless theres something I’m missing about  
“validates\_uniqueness\_of”, that only works within one table. How can I  
make my program check that the new screen name is unique to both tables?

Thanks for any help 🙂

---

<div class="post-metadata">

**Author:** ![lottario](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de053/32.png) [@lottario](https://www.ruby-forum.com/u/lottario)\
**Post date:** [May 9, 2008, 6:22pm UTC](https://www.ruby-forum.com/t/validating-uniqueness-from-two-database-tables/137167/2 "2008-05-09T18:22:37Z")

</div>

Hi Dan,

One way that springs to mind is:

class Organisation  
validate :validates\_uniqueness\_of\_screen\_name

def validates\_uniqueness\_of\_screen\_name  
in\_org = Organisation.find(:first, :conditions =\> [‘screen\_name  
= ?’, screen\_name])  
in\_user = in\_org ? nil : User.find(:first, :conditions =\>  
[‘screen\_name = ?’, screen\_name])  
errors.add(‘screen\_name’, ‘is already taken’) if in\_org or in\_user  
end  
end

class User  
validate :validates\_uniqueness\_of\_screen\_name

def validates\_uniqueness\_of\_screen\_name  
in\_org = Organisation.find(:first, :conditions =\> [‘screen\_name  
= ?’, screen\_name])  
in\_user = in\_org ? nil : User.find(:first, :conditions =\>  
[‘screen\_name = ?’, screen\_name])  
errors.add(‘screen\_name’, ‘is already taken’) if in\_org or in\_user  
end  
end

You could even add this in as a mixin if you feel inspired (that way  
you don’t repeat yourself) 🙂

Julian.

Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)  
VIDEO #4 parts a and b now available!  
[http://sensei.zenunit.com/](http://sensei.zenunit.com/)

---

<div class="post-metadata">

**Author:** ![lottario](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de053/32.png) [@lottario](https://www.ruby-forum.com/u/lottario)\
**Post date:** [May 9, 2008, 6:46pm UTC](https://www.ruby-forum.com/t/validating-uniqueness-from-two-database-tables/137167/3 "2008-05-09T18:46:49Z")

</div>

That works absolutely perfectly, thanks a bunch for your help Julian 🙂

---

<div class="post-metadata">

**Author:** ![lottario](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de053/32.png) [@lottario](https://www.ruby-forum.com/u/lottario)\
**Post date:** [May 9, 2008, 9:27pm UTC](https://www.ruby-forum.com/t/validating-uniqueness-from-two-database-tables/137167/4 "2008-05-09T21:27:10Z")

</div>

I think it was going to get messy no matter which way I did it. My  
original User classes had a lot of associations that I didn’t want my  
Organizations to have (for example, Users have an FAQ and a blog, which  
Organizations shouldn’t have).

It gets a little messy using the same log in and register forms for  
both, but I think in this case it works better to just use the two  
separate tables (then again, I’m fairly new to Rails and all, so I could  
very well be WAY off here).

---

<div class="post-metadata">

**Author:** ![lottario](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de053/32.png) [@lottario](https://www.ruby-forum.com/u/lottario)\
**Post date:** [May 9, 2008, 8:55pm UTC](https://www.ruby-forum.com/t/validating-uniqueness-from-two-database-tables/137167/5 "2008-05-09T20:55:24Z")

</div>

I have a feeling that this could get very, very messy. IMO you’re  
better off using single table inheritance in the User table.
