记录到期 [英] Record Expiration

查看:73
本文介绍了记录到期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

My Rails应用程序允许管理员向其他玩家提供违规信息,这些违规信息存储在数据库表中。这些违规行为的得分值加起来为玩家提供了得分值。但是,这些记录将包含到期时间戳。

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?

推荐答案

如何使用默认范围来自动删除违规记录?过滤掉过期的记录?

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

这样,您可以执行 Infraction.find(4),并且如果id == 4的违规行为已过期,则不会返回。

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

这篇关于记录到期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆