Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrap all controller actions in transactions in Rails

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?

like image 687
asc99c Avatar asked Dec 14 '11 16:12

asc99c


2 Answers

For info, I did this with an around_filter in my application controller:

around_filter :wrap_in_transaction

def wrap_in_transaction
  ActiveRecord::Base.transaction do
    yield
  end
end

This just rolls back the transaction on any unhandled exception, and re-raises the exception.

like image 192
asc99c Avatar answered Oct 04 '22 21:10

asc99c


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"
like image 31
Andrew Kuklewicz Avatar answered Oct 04 '22 21:10

Andrew Kuklewicz