The current implementation for calculating the percentage for a given option for a question is flawed:
percentage = ( 100 / question.votes.count ) * option.votes.count
This will break when rendering the statistics without any votes cast:
percentage = ( 100 / 0 ) * 0
# => ZeroDivisionError: divided by 0
As soon as the amount of votes surpasses 100 the result is always 0 due to integer division:
percentage = ( 100 / 2 ) * 1
# => 50
percentage = ( 100 / 100 ) * 50
# => 50
percentage = ( 100 / 101 ) * 100
# => 0
percentage = ( 100 / 200 ) * 100
# => 0
The quick'n'dirty fix is:
percentage = (( 100.0 / question.votes.count ) * option.votes.count).round rescue 0
This also applies to polls.
The current implementation for calculating the percentage for a given option for a question is flawed:
This will break when rendering the statistics without any votes cast:
As soon as the amount of votes surpasses 100 the result is always 0 due to integer division:
The quick'n'dirty fix is:
This also applies to polls.