Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8674761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:47:03+00:00 2026-06-12T19:47:03+00:00

I have been reading that ActiveRecord Transactions are automatically wrapped around save and destroy

  • 0

I have been reading that ActiveRecord Transactions are automatically wrapped around save and destroy actions. My question relates to the following scenario.

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.

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?

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 => shipments_url
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:47:04+00:00Added an answer on June 12, 2026 at 7:47 pm

    To elaborate on Mischa’s suggestion, ActiveRecord callbacks will allow you to improve this code in several important ways. First, it removes the ugly transaction block that prompted you to write this question. Almost always, if you see a transaction block like that in Rails, you’re probably doing something wrong. Secondly, it starts moving things back to where their concerns are. This will not only make maintenance easier, due to things being in intuitive locations and largely leveraging built-in Rails methods, but it will also make things a great deal easier to test.

    I suspect that there are not tests for this application. This might be a good excuse to write a few, as you’re making this adjustment. It’s starting to break down to the point where a few simple specs could go a long way, and your application complexity looks like it may be to the point where you could benefit from the added piece of mind (especially due to money being involved).

    I am going to make certain assumptions about your object’s relationships, and leave them out of the example code below, but I was imagining a has_many :through -type situation involving Shipments, Products, and ProductShipments.

    Your controller, cleaned up:

    def destroy
      # note: you may not need the additional scope of @shipment - depending on your view, you may be able to omit the @
      @shipment = Shipment.destroy(params[:id])
      respond_with @shipment, :location => shipments_url
    end
    

    Your Shipment model:

    class Shipment < ActiveRecord::Base
    
      before_destroy :restore_unshipped_inventory
    
      def restore_unshipped_inventory
        product_shipments.each(&:restore_unshipped_inventory)
      end
    
    end
    

    Your join model:

    class ProductShipment < ActiveRecord::Base  
    
      def restore_unshipped_inventory
        difference = -(s.qty_shipped.to_i)
        Product.update_inventory_quantities(difference, product.id)
      end
    
    end
    

    Honestly, I would even take it a little further, and make restore_unshipped_inventory in ProductShipment operate on the instance of product (product.update_inventory_quantities), even if all that instance method did was call the class method you have there, just to further isolate the unrelated logic.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been reading around and know that by default jQuery within a the
I have been reading that creating dependencies by using static classes/singletons in code, is
I have been reading that using HibernateDaoSupport isn't a good idea, so I need
I have been reading up on the changes that .NET4.5 will bring, and on
I have been reading about Quicksort and found that sometimes it' s referred to
As titled, how can I do that? I have been reading some Silverlight/WPF animation
I have been reading that direct access to a SQL Server database over the
i have been reading about a function that can overwrite its return address. void
I have been reading about JPA and EJB3 and would like to confirm that
I have been reading about css and js compression and minimizing. In particular that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.