我有一个带有 date_received、created_at 和 received_state 的发票模型。
创建记录后,我想比较 date_received 和 created_at 来更新 received_state。具体来说,如果 created_at 比 date_received 超过 2 天,received_state 应该等于 red。所以,像这样:
if created_at > date_received + 2 days
@invoice.update_attribute(:received_state, red)
else
@invoice.update_attribute(:received_state, green)
end
我知道有 date-in-words 辅助方法,但不确定如何在这种情况下使用它们。感谢您的任何帮助。
更新:我尝试了以下方法。没有错误,但记录不会在以下任何一种情况下更新:
if (:date_received - :created_at).to_i > 2
@invoice.update_attribute(:received_state, red)
else
@invoice.update_attribute(:received_state, green)
end
更新 2:我尝试了以下方法,即使满足绿色条件,记录也会更新为红色:
if :date_received.to_i - :created_at.to_i > 2
@invoice.update_attribute(:received_state, red)
else
@invoice.update_attribute(:received_state, green)
end