Reputation: 187154
Let's say I have a simple form in the administration of my Rails 2.x powered website.
<form action="/products/123">
Price:
<input type="text" name="product[price]" value="12.99"></input>
Description:
<textarea name="product[description]">
A long and descriptive block of text goes here.
</textarea>
<input type="submit"></input>
</form>
So the database is updated to contain what Sally had in the form, removing the price that Joe set while she was working with that page open.
Surely I am not the first one to stumble on a problem like this, but I've never had to deal with it. So first of all, does this sort of problem have a name? And second, what are some solutions to making it suck less?
A few solutions that come to mind, but some have serious drawbacks.
Don't save the record if the updated_at
timestamp was changed from when you opened the form. But what happens to the content you wanted to save? You'd have to copy the content you wanted to change out of the form, reload the edit form, and then paste it back in, hoping noone else edits anything in the meantime.
Forms load with most fields disabled, requiring the admin to "unlock" the fields they want to edit, and only the unlocked fields get sent to the server on submit. This greatly reduces the likelihood of conflicts in large forms, but adds a bunch of clicks to the content editing process, even it would be fine 99% of the time.
JS based solution that would do dirty field checking, disabling any form input that has the same value as when the form was loaded, so only changed fields are ever submitted, I like this option best so far, I guess. But it does sound a bit complex, and involves dumping my models to JSON on the page for the form submit handler to compare against.
But again, I'm sure I'm not the first person to have this problem. So is there a standard solution for this?
Upvotes: 0
Views: 410
Reputation: 9225
Have you checked this: http://api.rubyonrails.org/classes/ActiveRecord/Locking/Optimistic.html ?
UPDATE
Well, it doesn't really explain what to do if lock_version
is wrong, but at least it's better than updated_at
I guess. It all depends on who wins and why. If Sally didn't touch price it seems reasonable to update price from Joe's data when saving. Just add original values as hidden fields and compare them with Sally's input when saving in case lock_version
is wrong. If they both modified same field, well, no luck. Somebody should win, or you can go back to the edit page and highlight changes for acceptance.
Upvotes: 2