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?

有帮助吗?

解决方案

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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top