Note that there are some explanatory texts on larger screens.

plurals
  1. PORails / MySQL - saving the total of an order is doubling the line items
    primarykey
    data
    text
    <p>I'm working on an order entry module:</p> <p>Order has_many OrderLines</p> <p>Everything works fine, except when I add a line at the bottom to update the cached total on the order, this doubles the order lines created.</p> <p>a) I know it's not the transaction, this happens with and without it b) I know it has nothing to do with the "sub_total_ordered" method I apply to the order as I put in a set rate of 100 and it still did it. c) There are no before or after filters on either the Order or OrderLines.</p> <p>Any ideas?</p> <p>Edit* - I tried moving the @order.save to a OrderLine after_save filter and the problem goes away, but I still would like to know what is causing this.</p> <pre><code> def create @order = Order.find(params[:order_id]) OrderLine.transaction do params[:order_lines].each do |i, values| # If an orderline for the sku already exists find it and update, otherwise initialize it ol = @order.order_lines.find_or_initialize_by_style_number(values[:style_number], values) # If its a new record and the qty_ordered is &gt; 0 apply the order id then save it, otherwise delete it. if ol.new_record? &amp;&amp; values[:qty_ordered].to_i &gt; 0 ol.order_id = @order.id ol.save! # If updating an existing order line and the qty_ordered is &gt; 0 update it, otherwise delete it. else if values[:qty_ordered].to_i &lt; 1 ol.destroy else ol.update_attributes(values) end end end end # Update the cached_sub_total on the Order @order.cached_sub_total = @order.sub_total_ordered @order.save redirect_to order_path(@order) end </code></pre> <p>Here is the output (the bottom 4 lines is where you can see the orderlines inserted again.</p> <pre><code>Order Load (0.2ms) SELECT `orders`.* FROM `orders` WHERE `orders`.`id` = 1 LIMIT 1 (0.0ms) BEGIN OrderLine Load (0.3ms) SELECT `order_lines`.* FROM `order_lines` WHERE `order_lines`.`order_id` = 1 AND `order_lines`.`style_number` = 'F121' LIMIT 1 CACHE (0.0ms) SELECT `order_lines`.* FROM `order_lines` WHERE `order_lines`.`order_id` = 1 AND `order_lines`.`style_number` = 'F121' LIMIT 1 Sku Load (0.2ms) SELECT `skus`.* FROM `skus` WHERE `skus`.`sku_number` = 'F121' LIMIT 1 (0.2ms) UPDATE `skus` SET `qty_on_order` = 161, `updated_at` = '2012-03-20 02:56:01' WHERE `skus`.`id` = 1 SQL (0.1ms) INSERT INTO `order_lines` (`colour`, `created_at`, `order_id`, `price`, `product_size`, `qty_ordered`, `qty_shipped`, `style_number`, `updated_at`) VALUES ('Black Suede', '2012-03-20 02:56:01', 1, 10, NULL, 9, NULL, 'F121', '2012-03-20 02:56:01') OrderLine Load (0.1ms) SELECT `order_lines`.* FROM `order_lines` WHERE `order_lines`.`order_id` = 1 AND `order_lines`.`style_number` = 'F122' LIMIT 1 CACHE (0.0ms) SELECT `order_lines`.* FROM `order_lines` WHERE `order_lines`.`order_id` = 1 AND `order_lines`.`style_number` = 'F122' LIMIT 1 Sku Load (0.1ms) SELECT `skus`.* FROM `skus` WHERE `skus`.`sku_number` = 'F122' LIMIT 1 (0.1ms) UPDATE `skus` SET `qty_on_order` = 172, `updated_at` = '2012-03-20 02:56:01' WHERE `skus`.`id` = 2 SQL (0.1ms) INSERT INTO `order_lines` (`colour`, `created_at`, `order_id`, `price`, `product_size`, `qty_ordered`, `qty_shipped`, `style_number`, `updated_at`) VALUES ('Grey Suede', '2012-03-20 02:56:01', 1, 10, NULL, 9, NULL, 'F122', '2012-03-20 02:56:01') (0.4ms) COMMIT (0.2ms) SELECT SUM((ol.qty_ordered * ol.price)) AS sum_id FROM order_lines ol WHERE (ol.order_id = 1) (0.0ms) BEGIN SQL (0.2ms) INSERT INTO `order_lines` (`colour`, `created_at`, `order_id`, `price`, `product_size`, `qty_ordered`, `qty_shipped`, `style_number`, `updated_at`) VALUES ('Black Suede', '2012-03-20 02:56:01', 1, 10, NULL, 9, NULL, 'F121', '2012-03-20 02:56:01') SQL (0.1ms) INSERT INTO `order_lines` (`colour`, `created_at`, `order_id`, `price`, `product_size`, `qty_ordered`, `qty_shipped`, `style_number`, `updated_at`) VALUES ('Grey Suede', '2012-03-20 02:56:01', 1, 10, NULL, 9, NULL, 'F122', '2012-03-20 02:56:01') (0.4ms) COMMIT </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload