rails 为实例分配属性
rails assign attributes to instance
我正在计算我的交易总成本是多少。为了测试我是 运行 Transaction.last.calculate_total_payment
,它返回一个 BigDecimal
,这是我想要的,但是 none 之后的实例正在更新属性。我做错了什么?
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
您永远不会在 calculate_total_payment
方法中调用 save
。您需要显式保存对象才能将其持久保存在数据库中。
如果您想让您的更改持久保存在数据库中,您应该在最后调用 save!
。如果没有这个,它将修改对象(因此更改将在重新加载或再次获取对象后消失)。
我正在计算我的交易总成本是多少。为了测试我是 运行 Transaction.last.calculate_total_payment
,它返回一个 BigDecimal
,这是我想要的,但是 none 之后的实例正在更新属性。我做错了什么?
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
您永远不会在 calculate_total_payment
方法中调用 save
。您需要显式保存对象才能将其持久保存在数据库中。
如果您想让您的更改持久保存在数据库中,您应该在最后调用 save!
。如果没有这个,它将修改对象(因此更改将在重新加载或再次获取对象后消失)。