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



在每天结束时,我打算让这段代码将时间戳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,但是这个想法是习惯应该在第二天返回,这样用户每天都必须再次检查习惯。

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

你不需要在一天结束时将时间设置为nil,你需要将你的作用域更改为incomplete,这样它就会认为所有的习惯都是不完整的,而completed_at不是它的当前日期

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

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

这样你就不需要任何rake任务,你的逻辑将被用来考虑每天不完整的习惯。

优先级问题,尝试:

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

最新更新