In Ruby on Rails 3.2.1, I am iterating over the value pairs of a hash, where trsesh_mode is the key and trsesh_count is the value. I want to divide the trsesh_count for each mode of exercise the user had done by the total number of training sessions that user has completed.
Here is the iteration of the hash:
@trsesh_counts.each do |trsesh_mode, trsesh_count|
count = @user_name.training_sessions.count
@weight = trsesh_count / count
@weight is returning 0 for each value iterated into the calculation.
If I convert this calculation into a string and embed the ruby variables like so:
<% = "(#{trsesh_count}) / (#{count})" %>
… I get this output in my view:
(7) / (20)
(12) / (20)
(1) / (20)
where 20 is the count and the numerators are the iterated trsesh_count values.
If it helps, the trsesh_count values are FixNum.
My question is: Why am I getting 0 for output when performing the calculation above (@weight = …)? How can I fix this?
You are dividing integer numbers, so that the integer division result is zero.
Force one number to float, and it should work fine, like this: