2014-06-19 23 views
0

如果UserBooking已逾期,我希望User對於每個逾期的Booking每天收取1英鎊的罰款。爲逾期圖書館圖書建立懲罰系統 - 我如何記錄每位用戶的罰款?

到目前爲止,我所定義的逾期Booking作爲一個具有booking_end datetime屬性小於Time.now,仍然有一個active Boolean設置爲true

解決這個懲罰系統的正確方法是什麼?是否有必要爲罰款創建一個新表格?在這種情況下,我如何每天根據Booking進行罰款?

class Booking < ActiveRecord::Base 

    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 

end 

回答

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