我正在创建一个具有有效期的凭证系统。 expired
是一个整数,从创建日期到到期日期递减。例如,我在凭证的有效期expired
上放置了五天。
当日期改变时,我希望数字expires
[sic]减少1分。
我该怎么办?
答案 0 :(得分:0)
class Voucher < ApplicationRecord
# A method which will return days left count for expired date
def expiry_day_left
unless is_expired?
day_count = (Time.now.to_date - created_at.to_date).numerator
days_left = (expired - day_count)
return "#{days_left} left"
else
return "Voucher expired"
end
end
# A method which will return true or false if voucher has expired or not
def is_expired?
day_count = (Time.now.to_date - created_at.to_date).numerator
return day_count > expired ? true : false
end
end