#59 Optimistic Locking (revised)
Aug 04, 2012 | 8 minutes | Active Record
There is a chance one user will unintentionally override someone else's changes if they update a record near the same time. One solution is to use optimistic locking. The updated_at column can also be used for this as shown.
- source codeProject Files in Zip (97.2 KB)
- mp4Full Size H.264 Video (15.4 MB)
- m4vSmaller H.264 Video (8.71 MB)
- webmFull Size VP8 Video (11.4 MB)
- ogvFull Size Theora Video (18.6 MB)
Nice idea. It should be in rails like built-in validation. Thank's Ryan.
Sidequestion: Is it better to load modules into my models? I currently use abstract models for most things (which, however, loads modules itself). But I runned into problems as the
self.abstract_class = truegotcha seems to be not widely known...
The video is not working :(
Thanks so much as always! There is an episode for nearly every thing I want to do! Amazing!
Using the second method, I had a behavior with a remote form that I wanted to pass by the group. I got the first validation error just fine. If I hit submit again, however, the old value would pass the validation the second time and overwrite the value that was protected the first time around.
In other words, the conflict was not detected because @original_updated_at was set to nil and the object reloaded the new timestamp and passed the update right through. If I comment out the @original_updated_at = nil line, I get the behavior I want -- a durable block on any updates to that record.
Does this make sense? What was the value of setting @original_updated_at to nil in the code? Thanks for any advice you might have! Charles
One more quick discovery/question. With the second method, I was getting spurious locking errors added to base on creation. It turned out acts_as_list and other such plugins intercept the create process and perform some quick saves to get things in the right place. Not sure I completely understand, but I needed to pass the validation if nothing was in the changes array:
This got my simple creation action up and running again. Thanks for this great episode and any insights on whether this is a short sighted fix. Charles
One thing not mentioned in the episode is that HTTP reserves the 409 status code to indicate conflicts.
render status: :conflictalso works. Set it for your response, especially in APIs.
Hey Ryan, great screencast but I have one question. Say the user updates the value of the hidden field in the DOM for some weird reason (using inspect element). How can we account for this?
Great idea; works very well, is elegant and is set at the right places.
This was yet another awesome tutorial. I have chosen to go with your custom locking mechanism in my current project. Although I had to figure out my way through using an added time based field in the database as you mentioned at the end of the video, I did make one change to your handle_conflict method, changing the line that reads:
To handle cases where something was added to a blank field during edit.
By means of the subsequent technique, I had a performance with a distant shape that I required to go by by the collection. I got the primary corroboration mistake just well.
I simply want to tell you that I am new to weblog and definitely liked this blog site. I have been meaning to write something like this on my website and you have given me an idea
This is a very informative article.I was looking for these things and I found it here. I am doing a project and this information is very useful to me. I always try to find information and I hope I find out more about what I am for the blog website. For Cricket news.
Really great post, Thank you for sharing This knowledge.Excellently written article, if only all bloggers offered the same level of content as you, the internet would be a much better place.
hey dear thanks for posting this good information yes it can happen that one can override other can you give us a brief demo of optimized unlocking
Are you facing any kind of technical issues with your router device? don't worry, you can visit our site for help. our company provides solutions and services to the customers who are experiencing issues with their router device. they can visit our site and can interact with experts via support number which is also available on our website. so come to us and grab the best service in the market.