Product model Can be updated by multiple users. However, at a time only one user should able to update. For that, I want to use optimistic locking for Product model using updated_at.
I have implemented the following code for the optimistic lock. There are other ways to implement optimistic locking like using
lock_version. However, due to existing code restriction, I am not able to implement using
Product Model MAX_RETRIES = 3 attr_writer :original_updated_at def original_updated_at @original_updated_at || updated_at end def stale_object? if self.updated_at > original_updated_at @original_updated_at = nil return true else return false end end def public_method ##code result = nil begin result = private_method self.original_updated_at = self.updated_at rescue ActiveRecord::StaleObjectError => e errors.add(:base, ": Following records are changed while you were editing.") changes.except("updated_at").each do |name, values| errors.add(name, values) end rescue Exception => error ##code end result end def private_method tries = 0 begin raise ActiveRecord::StaleObjectError.new(self, "Product is changed while you were editing") if stale_object? attributes = #create hash using some code #some code attributes["updated_at"] = Time.now.utc Product.where(:id => self.id).update_all(attributes) self.original_updated_at = self.updated_at rescue ActiveRecord::StaleObjectError => e if tries < MAX_RETRIES tries += 1 sleep(1 + tries) self.reload retry else raise ActiveRecord::StaleObjectError.new(self, "Product is changed while you were editing") end end attributes end
✓ Extra quality
ExtraProxies brings the best proxy quality for you with our private and reliable proxies
✓ Extra anonymity
Top level of anonymity and 100% safe proxies – this is what you get with every proxy package
✓ Extra speed
1,ooo mb/s proxy servers speed – we are way better than others – just enjoy our proxies!
USA proxy location
We offer premium quality USA private proxies – the most essential proxies you can ever want from USA
Our proxies have TOP level of anonymity + Elite quality, so you are always safe and secure with your proxies
Use your proxies as much as you want – we have no limits for data transfer and bandwidth, unlimited usage!
Superb fast proxy servers with 1,000 mb/s speed – sit back and enjoy your lightning fast private proxies!
99,9% servers uptime
Alive and working proxies all the time – we are taking care of our servers so you can use them without any problems
No usage restrictions
You have freedom to use your proxies with every software, browser or website you want without restrictions
Perfect for SEO
We are 100% friendly with all SEO tasks as well as internet marketing – feel the power with our proxies
Buy more proxies and get better price – we offer various proxy packages with great deals and discounts
We are working 24/7 to bring the best proxy experience for you – we are glad to help and assist you!