# File app/models/smerf_forms_user.rb, line 68
  def SmerfFormsUser.update_records(smerf_form_id, user_id, responses)
    transaction do
      # Retrieve the response record for the current user
      smerf_forms_user = SmerfFormsUser.find_user_smerf_form(
        user_id, smerf_form_id)
      if (smerf_forms_user)
        # Update the record with the new responses 
        smerf_forms_user.update_attribute(:responses, responses)
        # Update response records, we use the activerecord replace method
        # First we find all the existing responses records for this form
        current_responses = SmerfResponse.find(:all, 
          :conditions => ["smerf_forms_user_id = ?", smerf_forms_user.id])  
        # Process responses, if the response has not changed or it's a new 
        # response add it to the array which we will pass to the replace method.
        # This method will add the new one's, and delete one's that are not in
        # the array
        new_responses = Array.new()
        responses.each do |question_response|
          # Check if response is a hash, if so process each response 
          # individually, i.e. each response to a question will have it's
          # own response record
          if (question_response[1].kind_of?(Hash) or 
            question_response[1].kind_of?(Array))
            question_response[1].each do |multichoice_response|
              if (multichoice_response.kind_of?(Hash) or 
                multichoice_response.kind_of?(Array))
                response = multichoice_response[1]
              else
                response = multichoice_response
              end
              if (!response.blank?())
                # Check if this response already exists, if so we add the 
                # SurveyResponse object to the array, otherwise we create a 
                # new response object
                #
                if (found = self.response_exist?(current_responses, 
                  question_response[0], response))
                  new_responses << found
                else  
                  new_responses << (SmerfResponse.new(
                    :smerf_forms_user_id => smerf_forms_user.id, 
                    :question_code => question_response[0],
                    :response => response))           
                end
              end
            end
          else       
              if (!question_response[1].blank?()) 
                # Check if this response already exists, if so we add the 
                # SurveyResponse object to the array, otherwise we create a 
                # new response object
                #
                if (found = self.response_exist?(current_responses, 
                  question_response[0], question_response[1]))
                  new_responses << found
                else  
                  new_responses << (SmerfResponse.new(
                    :smerf_forms_user_id => smerf_forms_user.id, 
                    :question_code => question_response[0],
                    :response => question_response[1]))
                end
              end
          end
        end
        # Pass the array that holds existing(unchanged) responses as well
        # as new responses to the replace method which will generate deletes
        # and inserts as required
        smerf_forms_user.smerf_responses.replace(new_responses)
      end
    end
  end