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

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -

delphi - How to convert bitmaps to video? -