如何在一天结束时将时间戳重置为 "nil"?

How to reset timestamp to "nil" at end of day?

在每天结束时,我打算让此代码将时间戳 completed_at 重置回 nil,但它不起作用。

def completed=(boolean)
  self.completed_at = boolean ? Time.current : nil
end

def completed
  completed_at && completed_at >= Time.current.beginning_of_day
end

有什么意义?

用户在认为他已经习惯了的情况下打勾:

<%= link_to ''.html_safe, mark_completed_path(habit), remote: true, method: 'put', class: 'update_habit' %>

习惯然后 fadeOut 但想法是习惯应该在第二天 return 这样用户每天都必须再次检查习惯。

def home
  @habits = current_user.habits.committed_for_today.incomplete.order(:order)
end

优先级问题,试试:

   def completed=(boolean)
      self.completed_at = (boolean ? Time.current : nil)
   end

您不需要在一天结束时将时间设置为 nil,您需要更改不完整的范围,以便它会认为 completed_at 不是当前日期的所有习惯都是不完整的

如果你想要所有习惯,其中 completed_at 小于当前日期。这样做

 scope: incompleted, -> {where("completed_at is null OR completed_at < ?", Date.current)}

这样您就不需要任何佣金任务,您的逻辑将用于考虑每天未完成的习惯。