views:

111

answers:

2

how to Handle version conflict when multiple concurrent editor on the same model rails? for example user A click edit on the post controller and start editing the content user B click edit on the post controller and start editing the title user B is done and click save user A is done and click save and override the modifications done by User B

is there to prevent that ? i'm thinking of using the updated_at field to check that the version edited is the last one. any plugin or gem to do that ??

+1  A: 

What you are looking for is a revision control or version control system for ActiveRecord. The Ruby Toolbox indexes several of them. laserlemon's vestal_versions looks like it would get the job done. I'm not sure if it handles version-conflicts out of the box, but could be done with a fork and some editing.

Benjamin Manns
+1  A: 

I'm not aware of any plugins or gems that take care of this directly (i.e. by handling everything from the model to the views), and have searched briefly on gemcutter.org and agilewebdevelopment.com for plugins to no avail.

One up and coming program/package that manages this very well is Google Wave.

But if that's not an option (as it almost certainly will not be), here's a quick and very simple idea that might get you there:

  1. Add a field to the 'edit' view called "requested_at"

  2. When you receive the 'update' for an edit, check whether the "requested_at" form field value is less than the stored object's "updated_at" value.

  3. If it is, send a response to the browser warning the user of the possible conflict. You could even display a difference between the user's submitted data and the current state of the stored object. For this 'diff' display I recommend the diff-lcs library available at gemcutter: http://gemcutter.org/gems/diff-lcs

  4. After the user views and revises his subimssion, rinse and repeat (or give them the option to override any existing changes even if someone else has edited the text).

An answer below mentioned the use of versioning for your active_record models, and for that you could try ActsAsAudited (at least that's my favorite)...but I'm not sure that's the problem you were describing.

btelles
Exactly what i was going to say. You could also do something like StackOverFlow - add some javascript on the client side to poll for updates and warn the user if the post has been modified.
klochner
Right on....using JS to poll for updates is an excellent idea.
btelles