user926958
user926958

Reputation: 9565

Rails Form submit race condition?

I am thinking whether it is possible that multiple users submitting a form causing data being overwritten. This is the flow of actions in the order of time:

1. User 1 clicks on Edit, rails render the page that has <form> and has value {A: 1, B: 2}
2. User 2 clicks on Edit, rails render another page that has <form> and has value {A: 1, B: 2}
3. User 1 updates value A to 5 and clicks on Submit, passing to server {A: 5, B: 2}
4. Server updates to database to {A: 5, B: 2}
5. User 2 updates value B to 10 and clicks on Submit, passing to server {A: 1, B: 10}
6. Server updates to database to {A: 1, B: 10}

Since User 2's browser had the old data. When B hit submit, the field A being past in as 1. User 1's update being overwritten.

How do I solve this issue in rails?

Upvotes: 0

Views: 134

Answers (2)

Richard_G
Richard_G

Reputation: 4820

If they are going to the same record, you are certainly exposed to data-loss due to race unless you check the record. You would want to check the record to ensure that it hasn't changed during the update process.

If you are generating a new record, then you should most likely depend upon the database to generate a unique record and id/key for it.

Upvotes: 1

MZaragoza
MZaragoza

Reputation: 10111

if you are using rails you should have the csrf_meta_tags this creates a unique key to let you know that form was submitted and is different for each user

Upvotes: 1

Related Questions