dimanche 22 octobre 2017

Rails 4 - Using Transactions along with Begin rescue block

i have a piece of code which is used to do following things triggered from a controller call.i am using Rails Transaction along with begin/rescue end block to make the below series of code execution more robust.Its as shown below:-

  1. Save the object
  2. save the associations and update the attribute value
  3. send email
  4. send text message
  5. redirect

    ###in my controller 
     def verify   
        @request=Request.includes(:user).find(params[:id])
        @request.transaction do
          begin
            if @request.accepted?
               ##this method will call more 3 methods in the request model
               @request.send_email_and_sms
               flash[:success] = "Request is moved to another state"
            else
              flash[:error] = "Request was not accepted and couldn't be moved to another state"
            end        
          rescue
            flash[:alert] = "There was some internal error.Kindly debug"
          ensure
            @request.reload
            Rails.logger.info "================Making GO AHEAD TOKEN FALSE AND DEACTIVATE VENUE====================#{@request.attributes}==========="
            redirect_to request_hall_path(@request.user)
          end      
        end    
      end
    
    

    `

Is this the correct way to ensure that every piece of code will execute else it will jump on rescue with the flash.alert message.

Is there any other way that i am missing to make this code/logic more full proof and robust.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire