title: Rails 3.2 ActiveRecord Transaction
body: <p>I have been reading that ActiveRecord Transactions are automatically wrapped around save and destroy actions.  My question relates to the following scenario.</p>
  
  <p>I have an inventory system that tracks Shipments and adjusts a Product's inventory amount when a shipment is created.  When I delete a shipment, I have it programmed that the shipment quantities are then added back to the Product's inventory amount.  This scenario is designed for if a user fouls up a shipment.  Deleting the shipment will then add the shipped items back into inventory.</p>
  
  <p>My question is when the product_shipments are looped through is it necessary to provide the Product.transaction block or can I omit this since the destroy method is automatically wrapped in a transaction?  Is it ok to wrap an entire loop in a transaction like I have? How can I best ensure that all these database operations are rolled back if something goes haywire?</p>
  
  <pre><code>def destroy
    @shipment = Shipment.find(params[:id])
    @shipments = @shipment.product_shipments
    Product.transaction do
      @shipments.each do |s|
        @difference = -(s.qty_shipped.to_i)
        Product.update_inventory_quantities(@difference, s.product_id)
      end
    end
    @shipment.destroy
    respond_with @shipment, :location =&gt; shipments_url
  end
  </code></pre>