user1367922
user1367922

Reputation: 229

Rails 3.2 observe a date attribute of a model

I have a model which saves several dates. Now I am looking for an permament observer which observes the dates and if one of this dates expires (in comparison to today's date), then I would like to perform some action (e.g. save this date to another model named ExpiredDate).

I saw the Rails Observer can only observe a Model after something new was created, deleted or updated. Is there any way how I can observe model attributes permamently?

Upvotes: 0

Views: 160

Answers (1)

Martin M
Martin M

Reputation: 8658

The reason, observers only work on create, update and delete is, that you need some trigger to start any action in Rails. Normally, that's a http request by a user.

To trigger actions on a time base, you could write a rake task or use rails/runner to execute some model method.

You then run the task or script with cron.
You can use a gem like whenever to handle the cron jobs. It also helps you, to set up the environtment to run ruby on rails.
Instead of

10 0 * * * /bin/bash -l -c 'cd /home/user/rail-app/releases/20130522173433 && RAILS_ENV=production bundle exec rake my_rake_task --silent'

it simplifies the configuration to

every :day, at: '0:10am' do
  rake 'my_rake_task'
end

Upvotes: 2

Related Questions