# Validates\_uniqueness\_of

**URL:** <https://www.ruby-forum.com/t/validates-uniqueness-of/48956>\
**Category:** Rails\
**Created:** [November 27, 2005, 8:10pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of/48956 "2005-11-27T20:10:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tmatsumoto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47774/32.png) [@tmatsumoto](https://www.ruby-forum.com/u/tmatsumoto)\
**Post date:** [November 27, 2005, 8:10pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of/48956/1 "2005-11-27T20:10:09Z")

</div>

Hi,

I’m trying to figure some stuff out about using  
validates\_uniqueness\_of, particularly if it is possible to validate a  
unique object in a database and rather than throwing an error like  
“name already exist”, use the primary key of the already existing  
object to pass along to join tables.

So far many of the examples I’ve come across has  
validates\_uniqueness\_of stopping if the object is not unique.

What happens if there is a table of authors joined to a table of  
articles, shouldn’t each author in the authors table be unique but  
still joined by its id if there is a new article by that author?

Thanks,

Todd

---

<div class="post-metadata">

**Author:** ![tmatsumoto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47774/32.png) [@tmatsumoto](https://www.ruby-forum.com/u/tmatsumoto)\
**Post date:** [November 27, 2005, 10:11pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of/48956/2 "2005-11-27T22:11:36Z")

</div>

tmatsumoto wrote:

> What happens if there is a table of authors joined to a table of  
> articles, shouldn’t each author in the authors table be unique but still  
> joined by its id if there is a new article by that author?

There’s a new find\_or\_create\_by\_X feature in the latest release  
candidate of Rails… I couldn’t find it in the ActiveRecord  
documentation, but DHH mentioned it in the Rails blog here:

[http://weblog.rubyonrails.com/articles/2005/11/07/rails-1-0-rc4-0-14-3-its-the-final-countdown](http://weblog.rubyonrails.com/articles/2005/11/07/rails-1-0-rc4-0-14-3-its-the-final-countdown)

Looks as if it is what you are looking for.

Justin

---

<div class="post-metadata">

**Author:** ![tmatsumoto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47774/32.png) [@tmatsumoto](https://www.ruby-forum.com/u/tmatsumoto)\
**Post date:** [September 19, 2008, 12:23pm UTC](https://www.ruby-forum.com/t/validates-uniqueness-of/48956/3 "2008-09-19T12:23:58Z")

</div>

Thanks Michael, you were right !

Have a nice day, Pierre.

On Sep 18, 12:26 pm, “Michael K.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![tmatsumoto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47774/32.png) [@tmatsumoto](https://www.ruby-forum.com/u/tmatsumoto)\
**Post date:** [September 19, 2008, 5:29am UTC](https://www.ruby-forum.com/t/validates-uniqueness-of/48956/4 "2008-09-19T05:29:21Z")

</div>

> Has anyone any ideas ? 🙂
> 
> Thank you very much,

Have you overridden the method exists? on that model? The default  
implementation takes one argument and your stacktrace implies that  
it’s expecting two. The uniqueness validation relies on this method  
now, which is why you didn’t see it earlier.

–  
Cheers

Koz
