为过期的图书馆书籍建立一个惩罚系统 - 我如何记录每个用户的罚款?

时间:2014-06-19 12:23:06

标签: ruby-on-rails ruby-on-rails-4

User的{​​{1}}过期时,我希望Booking每天为每个逾期User罚款1英镑。

到目前为止,我已将过期的Booking定义为Booking日期时间属性小于booking_end并且仍然将Time.now布尔值设置为{{1 }}

接近此惩罚系统的正确方法是什么?是否有必要为处罚创建一个新表?在这种情况下,我如何每天active每次罚款增加?

true

1 个答案:

答案 0 :(得分:1)

你不应该为此存储任何数据,它纯粹是计算的输出。只需在每次需要知道时实时计算罚金。

def overdue?
    due_date_absolute < Time.now.strftime("%d %B %Y") && self.active == true
end

def due_date_absolute
    self.booking_end.strftime("%d %B %Y")
end

def overdue_rate
  1.99 # $1.99 per day; hard-code this or pull it from a settings file, etc.
end

def fine
  # Calculate number of days over-due, multiply by overdue_rate
  overdue_rate * ((Time.now - self.booking_end) / 1.days)
end
相关问题