Working with decimals in Ruby on Rails 3

前端 未结 5 2056
夕颜
夕颜 2020-12-19 00:34

I\'m trying to calculate the average net price of a product. I have, in my Product model :total_sold and :total_net_revenue. Doing straight division in the method seems to a

5条回答
  •  长情又很酷
    2020-12-19 00:58

    Even if one of values are Float, result will be Float.

    Also if you using Rails and your data stores in specific model, ActiveRecord has special methods and you don't need to calculate by yourself.

    Model.average("field_with_data")
    

    Also available minimum, maximum, count, sum methods.

提交回复
热议问题