我有一家小型的鐵路店,並且幫助者計算總價和折扣(如果已應用),但我想確定實際折扣顯示在視圖 -Rails,從2名傭工處獲得折扣金額
def total_price
price = 0
order_products.each do |order_products|
# add the item price to total
price += (order_products.price_in_pence * order_products.quantity)
end
apply_discount(price)
end
def apply_discount(price)
return price if discount.nil?
if discount.percentage.present?
price = price - ((price/100) * discount.percentage)
elsif discount.money.present?
price = price - discount.money
end
price < 0 ? 0 : price
end
def discount_amount
I need something here to take the total_price from above
before a discount and then the apply_discount and subtract the 2.
end
'discount_amount'不能像其他兩個助手一樣寫入。您需要解決'total_price'和'apply_discount',因爲當前正在調用另一個。你無法得到不同的值。 – meagar 2014-10-07 07:02:05
您正在尋找物品/產品級別或訂單級別的折扣金額? – Surya 2014-10-07 07:10:31
另外,儘量避免:'order_products.each do | order_products |'。改爲將管道中的變量更改爲不同。就像:'order_products.each do | order_product |',這樣你就不會與你在塊內執行的操作混淆。 – Surya 2014-10-07 07:11:59