ruby on rails - How to simplify my model code? -
i new rails , wonder if there's way simplify code model:
class item < activerecord::base def subtotal if price , quantity price * quantity end end def vat_rate if price , quantity 0.19 end end def total_vat if price , quantity subtotal * vat_rate end end end
as far know *before_filter* not work within models?
i'd do:
class item < activerecord::base vat_rate = 0.19 def subtotal (price || 0) * (quantity || 0) end def total_vat subtotal * vat_rate end end
Comments
Post a Comment