Perform mathematical operations in Rails

0

So far I have only worked with information processing in Rails, companies, articles, products, etc. However there is something I have never done and curiously I do not find explicit information about it, but rather, directly in Ruby but not in Rails, clearly the operations must be the same, but the placement and treatment possibly different, going to the point, an example It would be easy to dynamically calculate the average grades of a student, taking into account that the first 3 notes each are worth 20%, and the final exam 40%, will pose more explicitly below:

Periodo1 //Tabla
----------------------------------------------
f.note_1 //field (20%)
f.note_2 //field (20%)
f.note_3 //field (20%)
f.final_examn //field (40%)
Promedio //Total

This occurs to me that could be done directly from the view:

<% @average %> = (<%= f.note_1 %>*(20%)) + (<%= f.note_2 %>*(20%)) + (<%= f.note_3 %>*(20%) + (<%= f.final_examn %>*(40%)

<%= @average %>

Although this may not be the most correct in the sense that the logic is not so recommended to do it from the view, I appreciate much can guide me and give me an example of how this part of Rails, Greetings!

    
asked by Hector Hernandez 13.08.2017 в 00:08
source

2 answers

0

Since the fields come from the model, it would be best to have a method in the model that gives you the average instead of calculating it in the view.

For example, assuming your model is named Student with the attributes note_1 , note_2 , note_3 , final_exam , you could do the following:

Model

class Student < ApplicationController
  PCT = { note_1: 0.2, note_2: 0.2, note_3: 0.2, final_exam: 0.4 }
  # asociaciones, validaciones, etc.

  def average()
    PCT.map { |note, pct| self[note] * pct }.reduce(:+) 
  end
end
  • PCT will keep the fixed values of the percentages (these could ideally go in a model if they can be changed later to avoid the need to modify the code).

Driver

@student = Student.find(1)

Vista

<%= @student.average %>
    
answered by 13.08.2017 / 05:31
source
0

1) Rails is 100% ruby. You can use all the options of this language + what this framework adds. In this case, everything you read about Ruby can be used in Rails.

2) The method you are using IS ALMOST CORRECT. The logic is correct but you are doing it wrong in two parts: First, instead of using 20%, use the floating 0.2. Second, everything should go inside ruby markers embedded in your view <%% & gt ;. Now you are leaving the mathematical operators out and therefore your formula does nothing. It should be this way (assuming that the local variable "f" that you use in your example exists):

<%= @final_score = f.note_1 * 0.2 + f.note_2 * 0.2 + f.note_3 * 0.2 + f.final_examn * 0.4 %>

3) The best practice would be to do this operation on the model or other object, but not on the view that you should not deal with this.

#Model
class ObjetoDondeEstanLasNotas < ApplicationController
  def final_score
    self.note_1 * 0.2 + self.note_2 * 0.2 + self.note_3 * 0.2 +  self.note_4 * 0.4
  end
end
    
answered by 15.08.2017 в 21:06