How to reset boolean to “default: false” at end of day?

前端 未结 3 1365
梦如初夏
梦如初夏 2021-01-16 08:41

I have a boolean in the DB: t.boolean \"completed\", default: false

I ONLY show those still false on the home page.<

3条回答
  •  囚心锁ツ
    2021-01-16 08:55

    I think you will actually want a rake task to reset them all, then just run it once a day with cron. Have a look at the whenever gem.

    You CAN do it without the rake task, but its a bit more difficult and inefficient, you will have to add a completed_timestamp, and set that to the current time whenever it is set to true. Then, whenever you fetch the model, check if the completed_timestamp is before today, and if it is, set it to false before you render your page. You could probably do this in an after_find callback.

    Edit: I highly recommend you go with the cron+rake task solution though, the second method is highly inefficient because you will have to fetch every record and check its timestamp every time you load the page. Its just a terrible solution, but I added it for completeness.

提交回复
热议问题