From 8da18792e68c71410e8c6ba31c8eb24bf13a407f Mon Sep 17 00:00:00 2001 From: francis Date: Tue, 11 Sep 2007 15:21:16 +0000 Subject: Tidy up the transaction. --- app/controllers/file_request_controller.rb | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) (limited to 'app/controllers/file_request_controller.rb') diff --git a/app/controllers/file_request_controller.rb b/app/controllers/file_request_controller.rb index 8424c83b0..b87fc4a9e 100644 --- a/app/controllers/file_request_controller.rb +++ b/app/controllers/file_request_controller.rb @@ -4,26 +4,33 @@ # Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. # Email: francis@mysociety.org; WWW: http://www.mysociety.org/ # -# $Id: file_request_controller.rb,v 1.5 2007-09-10 18:58:43 francis Exp $ +# $Id: file_request_controller.rb,v 1.6 2007-09-11 15:21:16 francis Exp $ class FileRequestController < ApplicationController def index end def create + # Create both FOI request and the first request message @info_request = InfoRequest.new(params[:info_request]) - @outgoing_message = OutgoingMessage.new(params[:outgoing_message].merge({ :status => 'ready', - :message_type => 'initial_request'})) + @outgoing_message = OutgoingMessage.new(params[:outgoing_message].merge({ + :status => 'ready', + :message_type => 'initial_request' + })) - InfoRequest.transaction do - @info_request.save! - @outgoing_message.info_request_id = @info_request.id - @outgoing_message.save! - # render create action + # Save both models + ActiveRecord::Base.transaction do + begin + @info_request.save! + @outgoing_message.info_request_id = @info_request.id + @outgoing_message.save! + # render create action + rescue ActiveRecord::RecordInvalid => e + @outgoing_message.valid? # force cecking of errors even if info_request fails + @outgoing_message.errors.full_messages.delete("info_request") + render :action => 'index' + end end - rescue ActiveRecord::RecordInvalid => e - @outgoing_message.valid? # force cecking of errors even if info_request fails - render :action => 'index' end end -- cgit v1.2.3