From d906a7342fb0f4261735eff62bd8302a19a58609 Mon Sep 17 00:00:00 2001 From: wvengen Date: Fri, 22 Aug 2014 09:35:05 +0200 Subject: [PATCH] workaround receive error with many articles --- app/controllers/orders_controller.rb | 31 ++++++++++++++-------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/app/controllers/orders_controller.rb b/app/controllers/orders_controller.rb index 8f9d9aa7..e31c1d9c 100644 --- a/app/controllers/orders_controller.rb +++ b/app/controllers/orders_controller.rb @@ -145,23 +145,24 @@ class OrdersController < ApplicationController # changed, rest_to_tolerance, rest_to_stock, left_over counts = [0] * 4 cunits = [0] * 4 - OrderArticle.transaction do - params[:order_articles].each do |oa_id, oa_params| - unless oa_params.blank? - oa = OrderArticle.find(oa_id) - # update attributes; don't use update_attribute because it calls save - # which makes received_changed? not work anymore - oa.attributes = oa_params - if oa.units_received_changed? - counts[0] += 1 - unless oa.units_received.blank? - cunits[0] += oa.units_received * oa.article.unit_quantity - oacounts = oa.redistribute oa.units_received * oa.price.unit_quantity, rest_to - oacounts.each_with_index {|c,i| cunits[i+1]+=c; counts[i+1]+=1 if c>0 } - end + # This was once wrapped in a transaction, but caused + # "MySQL lock timeout exceeded" errors. It's ok to do + # this article-by-article anway. + params[:order_articles].each do |oa_id, oa_params| + unless oa_params.blank? + oa = OrderArticle.find(oa_id) + # update attributes; don't use update_attribute because it calls save + # which makes received_changed? not work anymore + oa.attributes = oa_params + if oa.units_received_changed? + counts[0] += 1 + unless oa.units_received.blank? + cunits[0] += oa.units_received * oa.article.unit_quantity + oacounts = oa.redistribute oa.units_received * oa.price.unit_quantity, rest_to + oacounts.each_with_index {|c,i| cunits[i+1]+=c; counts[i+1]+=1 if c>0 } end - oa.save! end + oa.save! end end return nil if counts[0] == 0