Ruby on Rails: Percentage of Total does not work

Asked

Viewed 276 times

0

People need to calculate how many% of the Survivors (endpoint) were abducted and how many were not, but I have no idea how to do but it did not work, I do not know if I declared wrong also in Routes...I made this method here in model model

def percentage_abducted
if(abducted == true )
return abducted/servivor.all * 100
else
return abducted/servivor.all * 100
end
end

1 answer

0

You can’t exactly understand your logic for example your if and Else are doing the same thing. This servivor.all servivor is a model model?

If it is a model I believe that the best way would be to iterate the result of the servivor and do the calculation by line.

def percentage_abducted     
 if(abducted == true )
  calc = Array.new
  serv = servivor.all
  serv.each_with_index do |value, index|
    calc[index] = abducted/value*100
  end
 end
end

You do not need to use Return because ruby already understands that the last line executed is the return of the method.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.