# Database update how to prevent duplicates

**URL:** <https://www.ruby-forum.com/t/database-update-how-to-prevent-duplicates/128771>\
**Category:** Rails\
**Created:** [February 11, 2008, 12:49pm UTC](https://www.ruby-forum.com/t/database-update-how-to-prevent-duplicates/128771 "2008-02-11T12:49:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [February 11, 2008, 12:49pm UTC](https://www.ruby-forum.com/t/database-update-how-to-prevent-duplicates/128771/1 "2008-02-11T12:49:17Z")

</div>

Hi

database schema has items

login  
groupid

Now when database is updated how can I check if there is entry already  
with same information example:

new item is created with values login=doe groupid=10 and if such item  
exists already on database then error is returned ?

I believe on model you can do something like this, correct ?

I know on model you can check if login=doe is already there but I need  
to make sure that if both conditions  
apply at the same time.

---

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [February 11, 2008, 12:55pm UTC](https://www.ruby-forum.com/t/database-update-how-to-prevent-duplicates/128771/2 "2008-02-11T12:55:31Z")

</div>

validates\_uniqueness\_of would be the easiest way

[http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M001330](http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M001330)

---

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [February 11, 2008, 3:40pm UTC](https://www.ruby-forum.com/t/database-update-how-to-prevent-duplicates/128771/3 "2008-02-11T15:40:35Z")

</div>

On 2/11/08, Thorsten M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> validates\_uniqueness\_of would be the easiest way
> 
> [ActiveRecord::Validations::ClassMethods](http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M001330)

Note the commentary about creating a unique index if you want this to  
be safe in the face of concurrency.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
