我有些東西應該很簡單,但它會殺死我。導軌不能識別真假
l = LineItem.first
#<LineItem id: 5, product_id: 1, quantity: 1, price: #<BigDecimal:7f7fdb51a3f8,'0.999E3',9(18)>, cart_id: 5, discount_percentage: 10, discount_amount: nil, discount_active: true, created_at: "2012-01-12 16:17:41", updated_at: "2012-01-12 16:17:41">
我
l.discount_percentage.blank?
=> false
所以,我有以下方法:
def total_price
discount_amount = 0 if discount_amount.blank?
discount_percentage = 0 if discount_percentage.blank?
discounted_amount_from_percent = price*(discount_percentage.to_f/100)
applicable_discount = [discount_amount,discounted_amount_from_percent].max
return (price-applicable_discount)
end
但是,當我這樣做:
l.total_price
,而不是返回899,它返回999(意思是if discount_per centage.blank?根本沒有工作!)
或者語法WHATEVER_HERE如果真/假只能在View on Rails?
postfix條件('do_something if condition')是有效的Ruby語法,並且不限於視圖。 – MrTheWalrus 2012-01-12 16:48:19
如果使用'self.discount_percentage'代替會發生什麼? – 2012-01-12 16:53:13
戴夫,這工作,但問題是: 如果價格可以訪問沒有self.price,爲什麼我需要有自己的discount_amount和discount_percentage ??? – 2012-01-12 16:56:30