Rails: Reset a model attribute at specific time ea

2019-09-02 22:30发布

I have a rails app with a Location model, which has a rating, and a rating_count field. Now, I need to reset every Location's rating and rating_countattributes to 0 at a specific time everyday, lets say 12:00:00 UTC.

How would I accomplish this? I'm using the default sqlite3 databases.

3条回答
Bombasti
2楼-- · 2019-09-02 22:53

The best option is to use cron. You can find tons of documentation out there!Although if you are running a Rails app you should check out whenever a pretty neat gem for managing cron jobs for your app!

查看更多
放荡不羁爱自由
3楼-- · 2019-09-02 23:05

The easiest thing is to write a rake task that does that job, and then use whatever scheduling system your host uses (cron).

An alternative is to use delayed_job which allows to push work to a background process. While delayed-job is not exactly suited for something like this, it is perfectably capable of doing this. If your rails process starts, you add a new job, to run at 12:00. And the running of the job reschedules the job.

The nice thing of delayed-job is that your code runs in the context of a rails-process, so you can use methods you already have. Also nice: jobs are stored in the database, so you can have an overview.

查看更多
姐就是有狂的资本
4楼-- · 2019-09-02 23:05

If you're on a *nix box, write a script to do the updates (eg. in PHP, or Perl) and simply add it to crontab. Check out cron.

查看更多
登录 后发表回答