Note that there are some explanatory texts on larger screens.

plurals
  1. POMake Rails controller actions atomic?
    primarykey
    data
    text
    <p>Sometime one in a long series of events within a controller action fails. For example, a credit card is processed but then an ActiveRecord query times out. Is there any way to make those calls reversible?</p> <p>E.g. with this controller action:</p> <pre><code>def process_order cart = Cart.new(params[:cart]) load_order response = credit_card.charge if response submit_order order.receipt = Pdf.new(render_to_string(:partial =&gt; 'receipt') order.receipt.pdf.generate order.receipt.save render :action =&gt; 'finished' else order.transaction = response @message = order.transaction.message order.transaction.save render :action =&gt; 'charge_failed' end end </code></pre> <p>I would like to be able to put a block around it like so:</p> <pre><code>def process_order transaction cart = Cart.new(params[:cart]) load_order response = credit_card.charge if response submit_order order.receipt = Pdf.new(render_to_string(:partial =&gt; 'receipt') order.receipt.pdf.generate order.receipt.save render :action =&gt; 'finished' else order.transaction = response @message = order.transaction.message order.transaction.save render :action =&gt; 'charge_failed' end rollback credit_card.cancel_charge ... end end </code></pre> <p>This is just a contrived example and I'm not really sure how it would work in practice. What typically happens is we get an exception like <code>ActiveRecord::StatementInvalid: : execution expired</code> for the line with <code>submit_order</code> and then we have to go and manually run the rest of the lines that should have run. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload