Reputation: 67
I have a Grade model from which I need to pull the averages of two columns - accuracy and time. I’m using the following query to get both values together instead of querying the model twice.
Grade.where(student_id: 25).pluck('avg(accuracy), avg(time)')
I get two values alright, but they are in the following format which I do not understand:
[[#<BigDecimal:7f9c5a890410,’8.0’,9(18)>, #<BigDecimal:7f9c5a8903c0,’10.0’,9(18)>]]
How do I extract only the values that I need from this? i.e. avg_accuracy = 8.0 and avg_time = 10.0
I'm new to Ruby, Rails, ActiveRecord. I appreciate the help.
Upvotes: 0
Views: 1305
Reputation: 30056
grades = Grade.where(student_id: 25, class_id: 123).pluck('avg(accuracy), avg(time)').first
Should get
[#<BigDecimal:7f9c5a890410,’8.0’,9(18)>, #<BigDecimal:7f9c5a8903c0,’10.0’,9(18)>]
and now convert them to simple floats.
grades.map!(&:to_f)
should get
[8.0, 10.0]
Upvotes: 6