我正在尝试计算交易总成本的多少。并且要测试我正在运行Transaction.last.calculate_total_payment
,它返回BigDecimal
这是我想要的,但之后没有任何属性在实例上更新。我做错了什么?
class Transaction < ActiveRecord::Base
has_one :auction
has_one :bid
belongs_to :inventory_part
TIER0 = 0
TIER1 = 5_000
TIER2 = 50_000
TIER3 = 500_000
TIER4 = 1_000_000
.
.
.
.
def calculate_total_payment
part = self.bid.part_price
self.tax = part * self.tax_rate
if self.bid.tx.shipping_account
self.final_shipping_cost = 0
else # testing purposes
self.final_shipping_cost = self.bid.est_shipping_cost
end
price_before_fees = part + self.tax + self.final_shipping_cost
if price_before_fees < TIER1 #5,000
self.bid_aero_fee = price_before_fees * 0.025
self.armor_fee = price_before_fees * 0.015
elsif price_before_fees < TIER2 #50,000
self.bid_aero_fee = (price_before_fees - TIER1) * 0.015 + 125
self.armor_fee = (price_before_fees - TIER1) * 0.01 + 75
elsif price_before_fees < TIER3 #500,000
self.bid_aero_fee = (price_before_fees - TIER2) * 0.0125 + 800
self.armor_fee = (price_before_fees - TIER2) * 0.0075 + 525
elsif price_before_fees < TIER4 #1,000,000
self.bid_aero_fee = (price_before_fees - TIER3) * 0.0075 + 6425
self.armor_fee = (price_before_fees - TIER3) * 0.005 + 3900
else # anything over a million
self.bid_aero_fee = (price_before_fees - TIER4) * 0.0075 + 10175
self.armor_fee = (price_before_fees - TIER4) * 0.0035 + 6400
end
self.total_fee = self.armor_fee + self.bid_aero_fee
self.total_amount = price_before_fees + self.total_fee
end
end
答案 0 :(得分:0)
您永远不会在save
方法中调用calculate_total_payment
。您需要明确保存对象,以便将其保留在数据库中。
答案 1 :(得分:0)
如果要在数据库中保持更改,最后应调用save!
。如果没有这个,它将修改对象(因此重新加载再次获取对象后更改将消失)。