Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a model method at a specific time (Ruby on Rails)

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.

like image 958
Jon Lemmon Avatar asked Mar 29 '12 22:03

Jon Lemmon


1 Answers

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
like image 135
Harish Shetty Avatar answered Sep 30 '22 02:09

Harish Shetty