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 6932881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:45:41+00:00 2026-05-27T11:45:41+00:00

Is it possible to set up a Rails application such that all controller actions

  • 0

Is it possible to set up a Rails application such that all controller actions are automatically wrapped with a transaction, that gets automatically rolled back in case of unrescued exceptions?

I’m working on a Rails 3 application, currently for a fairly tricky action that makes numerous database changes. And I’ve been getting it wrong, lots of times! After a while I realised my code wasn’t working because I’d ended up with inconsistent data in the database.

I can easily enough wrap this with a transaction (it is a clear instance where one is needed!). However it got me thinking that, at least in development, it would be useful to apply this idea across every controller action.

Assuming it is possible, is there any downside to this?

  • 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-05-27T11:45:42+00:00Added an answer on May 27, 2026 at 11:45 am

    Can it be done? probably.
    Should it be done? probably not, otherwise this would be part of rails, or there would already be a great gem for this.

    If you have particular complex controller actions that are doing lots of db activity, and you want them to be in a transaction, my advice is to get this business logic and persistence into a model method, and put your transaction there. This also gives you more control for the cases where you may not always want this to happen.

    If you really, really want to do this anyway, I would bet you could do it with Rack middleware, like this (untested) one https://gist.github.com/1477287:

    # make this class in lib/transactional_requests.rb, and load it on start
    require 'activerecord'
    
    class TransactionalRequests
      def initialize(app)
        @app = app
      end
    
      def call(env)
        ActiveRecord::Base.transaction do
          @app.call(env)
        end
      end
    end
    
    # and in the app config
    config.middleware.use "TransactionalRequest"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my rails application I have a set of attachments that display, with a
Is possible to set an image as an iPhone application unique icon in code
Possible Duplicate: How do I set HTTP_REFERER when testing in Rails? I tried request.env[HTTP_REFERER]
I'm trying to setup a rails application so that I can choose between different
Is it possible in a Facebook application to set the Site URL to a
Is it possible with problems set up Rails 2.3.2 APP with mysql2 RubyGem and
I have a Rails 2.3 web application that uses the collection_select helper with :multiple
Using rails and active record, is it possible to set the id to a
How is it possible to keep my rails application up to date using chef?
In Java SE it is possible set the cause of an exception using initCause

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.