Reputation: 730
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?
Upvotes: 4
Views: 1933
Reputation: 363
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.
Upvotes: 5