如何重置布尔值"默认:假"在一天的结束? [英] How to reset boolean to "default: false" at end of day?
问题描述
我在DB一个布尔值: t.boolean已完成,默认值:false
I have a boolean in the DB: t.boolean "completed", default: false
我只显示那些仍然假
主页上。
I ONLY show those still false
on the home page.
如果用户检查一关:
<span class="glyphicon glyphicon-ok"><% :completed %></span>
完成变成真正
键,因此它从主页中消失。
Completed becomes true
and it therefore disappears from the home page.
在每一天,虽然年底,我们怎么能自动复位一切恢复默认:假
At the end of each day though, how can we automatically reset everything back to default: false
?
我创建一个模型法或范围做到这一点?
Do I create a model method or scope to do this?
推荐答案
我会使用时间戳,而不是一个布尔值。这有两个好处:
I would use a timestamp instead of a boolean. That has two benefits:
- 您知道当用户complated最后一次
- 您不需要通过一个cron作业在午夜重置价值。
的步骤是:首先从数据库中删除已完成
布尔列,并添加代替 completed_at
timestamp列
The steps would be: First remove the completed
boolean column from your database and add instead a completed_at
timestamp column.
二添加两个方法模型,允许比以前布尔列相同的行为。这意味着有不需要改变控制器或观点:
Second add two methods to your model that allow the same behaviour than the boolean column before. That means there is not need to change the controller or views:
def completed=(boolean)
self.completed_at = boolean ? Time.current : nil
end
def completed
completed_at && completed_at >= Time.current.beginning_of_day
end
这篇关于如何重置布尔值&QUOT;默认:假&QUOT;在一天的结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!