Record Expiration

喜夏-厌秋 提交于 2019-12-22 18:14:23

问题


My Rails app allows administrators to give other players infractions, which are stored in a database table. These infractions have point values that add up to give the player a points value. However, these records will contain an expiry time stamp. Is there any way of automatically deleting the infraction record when the expiry date has been reached?


回答1:


How about using a default scope that filters out expired records? Something like

class Infraction < ActiveRecord::Base

  default_scope -> { where("expired_at IS NULL OR expired_at < ?", Time.now) }

  before_create :set_expired_at

  private
  def set_expired_at
    self.expired_at = Time.now + 1.week
  end

end

This way, you can do Infraction.find(4), and if the infraction with id == 4 is expired, it won't be returned.



来源:https://stackoverflow.com/questions/20021461/record-expiration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!