This project is archived and is in readonly mode.

#3502 open
Arya Asemanfar

Reloading a new record results in an inconsistent state

Reported by Arya Asemanfar | November 22nd, 2009 @ 04:13 AM | in 2.3.10

If there is a race to create a new record for the same ID, and you try to reload, the resulting record is in an inconsistent state.

For example, if two processes do a find_or_initialize_by_id(5) and both come back with new records, then the first process saves, then the second tries and throws a ActiveRecord::RecordNotUnique. If you rescue that and do record.reload, it will load the record the first process saved, but without clearing the new_record flag. So if you try to save that record in the process that reloaded, it does an insert and throws another ActiveRecord::RecordNotUnique. The behavior is inconsistent, it should either not allow you to call reload on a new_record, or it should clear the new_record flag if the reload succeeded. The documentation for new_record? says that it returns false if a record for the object doesn't exist yet, which maybe indicates the fix should be to clear the new_record flag.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages