Dave Bixler Sat Mar 09 15:19:07 -0500 2013

Subject: Question about Fields that are Dirty

I am working on updating a system that was written a couple of years ago. The system was designed to only insert data, and never update. I'm adding the ability to update existing records which has led me to a question.

The data is loaded from a web page and then persisted to the database. I need the ability to, once the data is put into an object, to allow that object to see if it already exists in the database. If it does exist, then it needs to do an UPDATE and not an INSERT. Each object has a different method for finding itself in the database.

Is it possible to load an existing object, and then just copy the ID of that object into the NEW object, and do a ->save() to perform the UPDATE?

For example, if I have object FOO with three columns, COLA, COLB, and COLC, and I set the values of COLA, COLB, and COLC, then load an existing instance of a FOO object, can I copy that existing instance's ID into the NEW FOO object that already has the column values set so that doing the save() will cause it to UPDATE the existing instance in the database.

I hope my question wasn't too vague. Any help would be greatly appreciated.


Dave Bixler Sat Mar 09 16:18:28 -0500 2013

I answered my own question. This won't work because AR still sees the record as a "NEW" record.