# Save returns Mysql::Error: Duplicate entry

**URL:** <https://www.ruby-forum.com/t/save-returns-mysql-duplicate-entry/135490>\
**Category:** Rails\
**Created:** [April 22, 2008, 5:32pm UTC](https://www.ruby-forum.com/t/save-returns-mysql-duplicate-entry/135490 "2008-04-22T17:32:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [April 22, 2008, 5:32pm UTC](https://www.ruby-forum.com/t/save-returns-mysql-duplicate-entry/135490/1 "2008-04-22T17:32:20Z")

</div>

Hi all,

I use save on an ActiveRecord object to insert or update it. I expect  
save to insert a new object in the database when I didn’t assign the  
id to the object and update the object when I assigned an id to it.  
That’s what I think the documentation tells me:  
\* No record exists: Creates a new record with values matching  
those of the object attributes.  
\* A record does exist: Updates the record with values matching  
those of the object attributes.

Shouldn’t it update the record when the id already exists or have I  
misunderstood how save works?

regards,  
Stijn

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [April 22, 2008, 6:01pm UTC](https://www.ruby-forum.com/t/save-returns-mysql-duplicate-entry/135490/2 "2008-04-22T18:01:46Z")

</div>

yes, you misunderstood this detail  
when you create a new model object with eg User.new, Rails uses (I  
assume) an internal flag to determine, that this is a new record.  
If you load a record with User.find (eg) this flag is off.

So that’s a Rails internal state, setting the ID field has nothing to do  
with it, it only depends on the way the object was created.
