# ON DUPLICATE KEY update counter

**URL:** <https://www.ruby-forum.com/t/on-duplicate-key-update-counter/127507>\
**Category:** Rails\
**Created:** [January 29, 2008, 10:27pm UTC](https://www.ruby-forum.com/t/on-duplicate-key-update-counter/127507 "2008-01-29T22:27:23Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [January 29, 2008, 10:27pm UTC](https://www.ruby-forum.com/t/on-duplicate-key-update-counter/127507/1 "2008-01-29T22:27:23Z")

</div>

When my DB insert into MySQL is denied due to a unique field that is  
already populated, I want to update the field called “counter” for that  
record so I know how many times a duplicate record tried to perform the  
insert.

I know that MySQL has something called ON DUPLICATE KEY but I was hoping  
that there might already be something similar in Rails?

If not, I was thinking I would have to do something like:

- unless @record.save
- find the ID of the duplicate record
- add one to the value of the counter field for that record

Any ideas would be received warmly 🙂

jackster
