My Rails 应用程序允许管理员向其他玩家提出违规行为,这些违规行为存储在数据库表中。这些违规行为的分数加起来会给玩家一个分数。但是,这些记录将包含到期时间戳。有没有什么办法可以在到期后自动删除违规记录?
最佳答案
如何使用过滤掉过期记录的默认范围?就像是
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 的违规行为已过期,则不会返回。
关于ruby-on-rails - 记录到期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20021461/