Skip to content

Commit

Permalink
workaround receive error with many articles
Browse files Browse the repository at this point in the history
  • Loading branch information
wvengen committed Sep 2, 2014
1 parent 63d52db commit d906a73
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions app/controllers/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,23 +145,24 @@ def update_order_amounts
# 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
Expand Down

0 comments on commit d906a73

Please sign in to comment.