Save returns Mysql::Error: Duplicate entry

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

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs