我正在编写预订API,我希望在用户浏览预订应用时创建资产的临时保留。基本上,我需要一种方法来创建我的数据库中的临时记录,该记录在经过一段时间后过期。关于我应该怎么做的任何想法?
答案 0 :(得分:0)
class Reservation < ActiveRecord::Base
def reservation_has_ended
Time.now > reservation_datetime
end
# or...
def reservation_is_open
Time.now < reservation_datetime
end
# similarly...
scope is_open where(Time.now > reservation_datetime)
scope has_ended where(Time.now < reservation_datetime)
# ... but remember scopes are going away in rails 4.1...
end
然后我会有一个cron工作,比如小时或每天,
清理所有记录&gt; 1小时(或其他时期)。
您也可以将其作为手动作业,通过控制器和模型中的更新操作从视图链接触发,例如,
# Reservation View
<%= link_to("Purge records", Reservation.purge) %>
# Reservation Controller
def remove_old_reservations
Reservation.remove_old_reservations
end
# Reservation Model:
def remove_old_reservations
self.delete_all.where((Time.now - 1.hour) > reservation_datetime)
end