我有一个CommercialDocument
具有discount_amount
属性和discount_amount_with_tax
虚拟属性的模型。
这是我在模型中定义的方式:
def discount_amount_with_tax
discount_amount * (1 + tax.rate / 100)
end
def discount_amount_with_tax=(amount)
self.discount_amount = amount.to_f / (1 + tax.rate / 100)
end
在我的表格中,用户可以同时填写discount_amount
和discount_amount_tax
:
= f.label :discount_amount
= f.text_field :discount_amount
= f.text_field :discount_amount_with_tax
我想优先考虑该discount_amount_with_tax
字段,这意味着discount_amount
除非其他字段为空,否则不得考虑。
我的问题是,如果我什么都不放在discount_amount_with_tax
字段中,比如说 10 in discount_amount
,那么discount_amount
将等于 0,这显然不是我想要的。
我怎样才能解决这个问题 ?
任何帮助将不胜感激。