I have a Ruby on Rails model that has a column called expiration_date
. Once the expiration date is reached, I want another column on the model to be modified (e.g. expired = true
). What are some good ways of doing this?
Ideally I'd like a model function to be called at the exact moment the expiry date is reached.
Use delayed_job
gem. After installing delayed_job gem do the following:
class Model < ActiveRecord::Base
after_create :set_expiry_timer
# register the timer
def set_expiry_timer
delay(:run_at => expiration_date).expire
end
def expire
update_attribute(:expired, true) unless expired?
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With