This project is archived and is in readonly mode.
Reloading a new record results in an inconsistent state
-
Matt Jones
- Tag cleared.
Why on earth are you trying to pick an ID for the record before it's saved? The whole situation has "race condition disaster" written all over it.
-
Jeremy Kemper
- Tag set to activerecord, reloading
- Assigned user set to Jeremy Kemper
- State changed from new to open
- Milestone set to 2.3.6
Yes, indeed. Patch welcome for next 2.3 release!
-
Jeremy Kemper
Matt, the scenario does have code smell, but it's broken nonetheless :)
-
Arya Asemanfar
- Assigned user cleared.
@matt Facebook application, we use their facebook user id as the id column. When you interact with a friend on the app, their record is created by their ID.
-
Arya Asemanfar
I should clarify that the ID column is not an auto-increment column.
-
Arya Asemanfar
- Tag changed from activerecord, reloading to activerecord, patch, reloading
- Assigned user set to Jeremy Kemper
Here is a patch and test for this ticket.
My earlier comment cleared the assigned user because I had loaded the form before it was assigned, so I'm assigning it back to Jeremy. Sorry about that.
-
Rizwan Reza
- Tag changed from activerecord, patch, reloading to activerecord, bugmash, patch, reloading
-
Anil Wadghule
-1
Without the fix, test doesn't fail on Rails master and Rails 2-3 stable.
-
Bastes
Looks fixed already, patch does nothing.
-
Arya Asemanfar
Patch doesn't apply anymore because the relevant code was moved from base.rb to persistence.rb, but the problem still exists. I just tried the test on master and it's still broken. Looking through the code also confirms that new_record is not set to false after successful reload, and save checks "new_record?" and then chooses "create" which does an insert.
-
Colin Casey
+1 test does fail on master.
Rewrote Arya's patch so it applies to master. I've attached a patch and I've attached a test.
-
Colin Casey
- Tag changed from activerecord, bugmash, patch, reloading to activerecord, bugmash, bugmash-review, patch, reloading
-
David Trasbo
IMHO, the correct way to fix this would be to not allow reloading of new records at all. After all, the whole idea of reloading is to re-fetch the record from the database and bring it back in sync with the reloaded object. Since a new record is not yet in the database, reloading does not make sense conceptually.
-
Andrea Campi
-1 I agree with David, I don't see the use of reloading a new record.
-
Arya Asemanfar
I don't disagree. In fact that solution was stated in my original ticket. The reason I patched it the way I did was because my particular use case needed me to fetch the record I thought was new, but wasn't, from the database. So if I didn't do it that way, I'd have to do something like record.replace(Model.find(record.id)).
Whether or not use you a non-auto-increment PK is another argument, but that case is clearly supported. So in conclusion, it's still inconsistent, and either solution will resolve the inconsistency.
-
Andrea Campi
As far as I'm concerned, I'd rather see #reload raise if passed a new_record?
-
Ryan Bigg
Automatic cleanup of spam.
