ActionController w/ transactions by HTTP Verb

In the ActionController docs there is a snippet about wrapping controller actions in a database transaction.

class ChangesController < ApplicationController
  around_action :wrap_in_transaction, only: :show
 
  private
 
  def wrap_in_transaction
    ActiveRecord::Base.transaction do
      begin
        yield
      ensure
        raise ActiveRecord::Rollback
      end
    end
  end
end

Now that multiple databases is supported in Rails and the toggling of the primary/replica is done in a middleware based on the HTTP verb, I wanted to propose a similar pattern for supporting transactions when the HTTP verb is used to mutate date. I was thinking this would be an optional middleware that can be configured to be on or off. Thoughts?

1 Like