title: Putting logic into a Ruby on Rails application: calculate a value for a field when creating a new row of the database
body: <p>I'm fairly new to Ruby on Rails. I'm building a web app that will ask a patient a series of questions, then calculate a risk using a previously validated model. When a new patient record is created, I want the application to take the data the user entered into the other fields, and use it to calculate the value of the last field. I have the logic written out, but just don't know where to actually code it into my application.</p>
  
  <p>Should it go in the model, view or controller? And how should I go about including it?</p>
  
  <p>My git repo:
  <a href="https://github.com/Midazolam/anaesthesia" rel="nofollow noreferrer">https://github.com/Midazolam/anaesthesia</a></p>
  
  <p>This is my view patients/form.html.erb</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;%= form_with(model: patient, local: true) do |form| %&gt;
    &lt;% if patient.errors.any? %&gt;
      &lt;div id="error_explanation"&gt;
        &lt;h2&gt;&lt;%= pluralize(patient.errors.count, "error") %&gt; prohibited this patient from being saved:&lt;/h2&gt;
  
        &lt;ul&gt;
        &lt;% patient.errors.full_messages.each do |message| %&gt;
          &lt;li&gt;&lt;%= message %&gt;&lt;/li&gt;
        &lt;% end %&gt;
        &lt;/ul&gt;
      &lt;/div&gt;
    &lt;% end %&gt;
  
    &lt;div class="form-group"&gt;
      &lt;%= form.label :"First Name" %&gt;&lt;br&gt;
      &lt;%= form.text_field :firstName, class: "form-control", id: :patient_firstName %&gt;
    &lt;/div&gt;
    &lt;hr&gt;
  
    &lt;div class="form-group"&gt;
      &lt;%= form.label :"Surname" %&gt;&lt;br&gt;
      &lt;%= form.text_field :surName, class: "form-control", id: :patient_surName %&gt;
    &lt;/div&gt;
    &lt;hr&gt;
  
    &lt;div class="form-group"&gt;
      &lt;%= form.label :"Date of Birth" %&gt;&amp;nbsp &amp;nbsp 
      &lt;%= form.date_select :dob, :start_year=&gt;1900,:end_year=&gt;2030, id: :patient_dob %&gt;
    &lt;/div&gt;
    &lt;hr&gt;
  
    &lt;div class="form-group"&gt;
      &lt;%= form.label :"Gender" %&gt;&amp;nbsp &amp;nbsp 
      &lt;%= form.select :gender, ["","male", "female"], id: :patient_gender %&gt;
    &lt;/div&gt;
    &lt;hr&gt;
  
    &lt;div class="form-group"&gt;
      &lt;%= form.label :"Which hospital are you having the opperation in?" %&gt;&amp;nbsp &amp;nbsp 
      &lt;%= form.select :hospital, ["","Beaumont", "Connelly", "St. Josephs", "Naas", "Limerick"], id: :patient_hospital %&gt;
    &lt;/div&gt;
    &lt;hr&gt;
  
    &lt;div class="form-group"&gt;
      &lt;%= form.label :"What date are you having the opperation on?" %&gt;&amp;nbsp &amp;nbsp 
      &lt;%= form.date_select :opDate, :start_year=&gt;2017,:end_year=&gt;2027, id: :patient_opDate %&gt;
    &lt;/div&gt;
    &lt;hr&gt;
  
    &lt;div class="form-group"&gt;
      &lt;%= form.label :"How would you rate your overall health" %&gt;&lt;br&gt;
      &lt;%= form.select :q1, ["","No abnormalities", "Better than expected", "Worse  than expected", "Dementia", "Sick", "Moribund"], id: :patient_q1 %&gt;
    &lt;/div&gt;
    &lt;hr&gt;
  
    &lt;div class="form-group"&gt;
      &lt;%= form.label :"How is your physical condition in general?" %&gt;&lt;br&gt;
      &lt;%= form.select :q2, ["","I can do everything I want", "Slightly impaired, but I feel no impairments in daily activities", "Impaired, I can only take care of domestic chores", "I am completely dependent on help from others"],  id: :patient_q2 %&gt;
    &lt;/div&gt;
    &lt;hr&gt;
  
    &lt;div class="form-group"&gt;
      &lt;%= form.label :"Do you have a heart condition that impedes your physical efforts (as a result of tiredness, pain or shortness of breath)?" %&gt;&lt;br&gt;
      &lt;%= form.select :q3, ["","No", "Yes, only during heavy labour", "Yes, during climbing the stairs, in the wind, stress", "Yes, daily, not at rest", "Yes, at rest and increased during any activity"], id: :patient_q3 %&gt;
    &lt;/div&gt;
    &lt;hr&gt;
  
    &lt;div class="form-group"&gt;
      &lt;%= form.label :"Have you ever experienced any feeling of pain or tightness in the chest?" %&gt;&lt;br&gt;
      &lt;%= form.select :q4, ["","No", "Yes, only during heavy labour", "Yes, during climbing the stairs, in the wind, stress", "Yes, daily, not at rest", "Yes, at rest and increased during any activity"], id: :patient_q4 %&gt;
    &lt;/div&gt;
    &lt;hr&gt;
  
    &lt;div class="form-group"&gt;
      &lt;%= form.label :"Have you ever had a heart attack?" %&gt;&lt;br&gt;
      &lt;%= form.select :q5, ["","No", "Yes, &gt; 12 months ago", "Yes, &gt; 6 and &lt; 12 months ago", "Yes, &gt; 3 and &lt; 6 months ago", "Yes, &lt; 3 months ago"], id: :patient_q5 %&gt;
    &lt;/div&gt;
    &lt;hr&gt;
  
    &lt;div class="form-group"&gt;
      &lt;%= form.label :"Have you ever had a cardiac arrest?" %&gt;&lt;br&gt;
      &lt;%= form.select :q6, ["","No", "Yes, &gt; 12 months ago", "Yes, &gt; 6 and &lt; 12 months ago", "Yes, &gt; 3 and &lt; 6 months ago", "Yes, &lt; 3 months ago"], id: :patient_q6 %&gt;
    &lt;/div&gt;
    &lt;hr&gt;
  
    &lt;div class="form-group"&gt;
      &lt;%= form.label :"Have you ever undergone any cardiac surgery or heart catheterization?" %&gt;&lt;br&gt;
      &lt;%= form.select :q7, ["","No", "Yes, &gt; 6 months ago", "Yes, &gt; 3 and &lt; 6 months ago", "Yes, &gt; 6 weeks and &lt; 3 months ago", "Yes, &lt; 6 weeks ago"], id: :patient_q7 %&gt;
    &lt;/div&gt;
    &lt;hr&gt;
  
    &lt;div class="form-group"&gt;
      &lt;%= form.label :"Do you ever get palpitations?" %&gt;&lt;br&gt;
      &lt;%= form.select :q8, ["","No", "Yes, I have no treatment", "Yes, I am treated with medication"], id: :patient_q8 %&gt;
    &lt;/div&gt;
    &lt;hr&gt;
  
    &lt;div class="form-group"&gt;
      &lt;%= form.label :"Have you been told you have a heart murmur?" %&gt;&lt;br&gt;
      &lt;%= form.select :q9, ["","No", "Yes, but no operation", "Yes, I have had an operation"], id: :patient_q9 %&gt;
    &lt;/div&gt;
    &lt;hr&gt;
  
    &lt;div class="form-group"&gt;
      &lt;%= form.label :"Do you have high blood pressure?" %&gt;&lt;br&gt;
      &lt;%= form.select :q10, ["","No", "I don't know", "Yes"], id: :patient_q10 %&gt;
    &lt;/div&gt;
    &lt;hr&gt;
  
    &lt;div class="form-group"&gt;
      &lt;%= form.label :"Do you have high cholesterol?" %&gt;&lt;br&gt;
      &lt;%= form.select :q11, ["","No", "Yes"], id: :patient_q11 %&gt;
    &lt;/div&gt;
    &lt;hr&gt;
  
    &lt;div class="form-group"&gt;
      &lt;%= form.label :"Do you receive treatment for diabetes (high blood sugar level)?" %&gt;&lt;br&gt;
      &lt;%= form.select :q12, ["","No", "Yes, treatment with diet", "Yes, treatment with oral medication", "Yes, treatment with insulin"], id: :patient_q12 %&gt;
    &lt;/div&gt;
    &lt;hr&gt;
  
    &lt;div class="form-group"&gt;
      &lt;%= form.label :"Have you ever had a stroke?" %&gt;&lt;br&gt;
      &lt;%= form.select :q13, ["","No", "Yes, but complete recovery", "Yes, with physical impairment/ disability", "Yes, &lt; 2 months ago"], id: :patient_q13 %&gt;
    &lt;/div&gt;
    &lt;hr&gt;
  
    &lt;div class="form-group"&gt;
      &lt;%= form.label :"Do you receive treatment for asthma?" %&gt;&lt;br&gt;
      &lt;%= form.select :q14, ["","No", "Yes, sometimes", "Yes, often", "Yes, daily", "Yes, daily and increased over the last six months"], id: :patient_q14 %&gt;
    &lt;/div&gt;
    &lt;hr&gt;
  
    &lt;div class="form-group"&gt;
      &lt;%= form.label :"Do you receive treatment for pulmonary emphysema, chronic obstructive pulmonary disease (COPD) or chronic bronchitis?" %&gt;&lt;br&gt;
      &lt;%= form.select :q15, ["","No", "Yes, only during heavy labour", "Yes, always during climbing the stairs, in the wind, stress", "Yes, always even at rest, I need oxygen therapy"], id: :patient_q15 %&gt;
    &lt;/div&gt;
    &lt;hr&gt;
  
    &lt;div class="form-group"&gt;
      &lt;%= form.label :"Do you have Sleep Apnoea?" %&gt;&lt;br&gt;
      &lt;%= form.select :q16, ["","No", "Yes"], id: :patient_q16 %&gt;
    &lt;/div&gt;
    &lt;hr&gt;
  
    &lt;div class="form-group"&gt;
      &lt;%= form.label :"Do you have any renal problems or kidney impairment?" %&gt;&lt;br&gt;
      &lt;%= form.select :q17, ["","No", "Yes, but with complete recovery", "Yes, treatment with fluid restriction only", "Yes, treatment with dialysis", "Yes, other serious kidney condition (e.g. kidney resection)"], id: :patient_q17 %&gt;
    &lt;/div&gt;
    &lt;hr&gt;
  
    &lt;div class="form-group"&gt;
      &lt;%= form.label :"Have you ever had jaundice or any liver problems?" %&gt;&lt;br&gt;
      &lt;%= form.select :q18, ["","No", "Yes, but with complete recovery", "Yes, liver cirrhosis has been diagnosed", "Yes, other serious liver condition (e.g. partial resection)"], id: :patient_q18 %&gt;
    &lt;/div&gt;
    &lt;hr&gt;
  
    &lt;div class="form-group"&gt;
      &lt;%= form.label :"Have you ever unintentionally lost more than 10 kg in a short period of time?" %&gt;&lt;br&gt;
      &lt;%= form.select :q19, ["","No", "Yes", "Yes, more than 25% of previous bodyweight"], id: :patient_q19 %&gt;
    &lt;/div&gt;
    &lt;hr&gt;
  
    &lt;div class="form-group"&gt;
      &lt;%= form.label :"Do you smoke?" %&gt;&lt;br&gt;
      &lt;%= form.select :q20, ["","No", "Yes"], id: :patient_q20 %&gt;
    &lt;/div&gt;
    &lt;hr&gt;
  
    &lt;div class="form-group"&gt;
      &lt;%= form.label :"Do you consume alcohol?" %&gt;&lt;br&gt;
      &lt;%= form.select :q21, ["","No", "Yes, &lt; 3 Units/day", "Yes, &gt; 3 and &lt; 6 Units/day", "Yes, &gt; 6 Units/day"], id: :patient_q21 %&gt;
    &lt;/div&gt;
    &lt;hr&gt;
  
    &lt;div class="form-group"&gt;
      &lt;%= form.label :"Do you ever take illicit drugs (Cocaine, Heroin, Ecstasy)?" %&gt;&lt;br&gt;
      &lt;%= form.select :q22, ["","No", "No, not anymore", "Yes, every once in a while", "Yes, more the once a week"], id: :patient_q22 %&gt;
    &lt;/div&gt;
    &lt;hr&gt;
  
  
  
  
  
    &lt;div class="form-group"&gt;
      &lt;%= form.label :"ASA grade" %&gt;&lt;br&gt;
      &lt;%= form.select :asa, ["","1", "2", "3", "4", "5"], id: :patient_asa %&gt;
    &lt;/div&gt;
    &lt;hr&gt;
  
    &lt;div class="form-group"&gt;
      &lt;%= form.submit "Submit for Assessment", class: "btn btn-primary" %&gt;
    &lt;/div&gt;
  &lt;% end %&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>And this is my patient controller</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>class PatientsController &lt; ApplicationController
    before_action :set_patient, only: [:show, :edit, :update, :destroy]
  
    # GET /patients
    # GET /patients.json
    def index
      @patients = Patient.all
    end
  
    # GET /patients/1
    # GET /patients/1.json
    def show
      
    end
  
    # GET /patients/new
    def new
      @patient = Patient.new
    end
  
    # GET /patients/1/edit
    def edit
    end
  
    # POST /patients
    # POST /patients.json
    def create
      @patient = Patient.new(patient_params)
      respond_to do |format|
        if @patient.save
          format.html { redirect_to @patient }
          format.json { render :show, status: :created, location: @patient }
        else
          format.html { render :new }
          format.json { render json: @patient.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # PATCH/PUT /patients/1
    # PATCH/PUT /patients/1.json
    def update
      respond_to do |format|
        if @patient.update(patient_params)
          format.html { redirect_to @patient, notice: 'Patient was successfully updated.' }
          format.json { render :show, status: :ok, location: @patient }
        else
          format.html { render :edit }
          format.json { render json: @patient.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # DELETE /patients/1
    # DELETE /patients/1.json
    def destroy
      @patient.destroy
      respond_to do |format|
        format.html { redirect_to patients_url, notice: 'Patient was successfully destroyed.' }
        format.json { head :no_content }
      end
    end
  
    private
      # Use callbacks to share common setup or constraints between actions.
      def set_patient
        @patient = Patient.find(params[:id])
      end
  
      # Never trust parameters from the scary internet, only allow the white list through.
      def patient_params
        params.require(:patient).permit(:firstName, :surName, :dob, :gender, :hospital, :opDate, :q1, :q2, :q3, :q4, :q5, :q6, :q7, :q8, :q9, :q10, :q11, :q12, :q13, :q14, :q15, :q16, :q17, :q18, :q19, :q20, :q21, :q22, :asa)
      end
  end</code></pre>
  </div>
  </div>
  </p>
  
  <p>Thank you!</p>