컨트롤러 동작 내에서 긴 일련의 이벤트 중 하나가 실패합니다. 예를 들어 신용 카드가 처리되었지만 ActiveRecord 쿼리가 시간 초과되었습니다. 그 전화를 되돌릴 수있는 방법이 있습니까?Rails 컨트롤러 작업을 원 자성으로 만드시겠습니까?
예. 이 컨트롤러 액션과 :
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 => 'receipt')
order.receipt.pdf.generate
order.receipt.save
render :action => 'finished'
else
order.transaction = response
@message = order.transaction.message
order.transaction.save
render :action => 'charge_failed'
end
end
내가 지금처럼 주위에 블록을 넣을 수 있도록하고 싶습니다
:
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 => 'receipt')
order.receipt.pdf.generate
order.receipt.save
render :action => 'finished'
else
order.transaction = response
@message = order.transaction.message
order.transaction.save
render :action => 'charge_failed'
end
rollback
credit_card.cancel_charge
...
end
end
이 그냥 인위적인 예입니다 그리고 난 정말 모르겠어요 어떻게 것 실제로 일한다. 일반적으로 발생하는 것은 submit_order
이있는 행에 대해 ActiveRecord::StatementInvalid: : execution expired
과 같은 예외가 발생하면 실행해야하는 나머지 행을 수동으로 실행해야합니다.
아주 좋네요. 이 솔루션을 살펴보면 동료가 상태 시스템을 사용하도록 제안했습니다. 이것은 특정 상황에서 사용될 수 있지만 이것은 좋은 일반적인 해결책입니다. –