【发布时间】:2012-01-25 21:33:25
【问题描述】:
我有一个自定义验证方法,可以确保以后只能设置警报..
class Alarm < ActiveRecord::Base
validate :alarm_set_in_the_future
def alarm_set_in_the_future
if time <= DateTime.current
errors.add(:alarm, "Please Choose A Future Date & Time")
end
end
这很好用,测试在创建新警报记录时通过。太棒了...
但是,在编辑警报时,time 变量被设置为以下日期时间,我不知道为什么!
2000-01-01 03:28:00 UTC
因此,验证失败。
时间参数在哪里重置?救命!
【问题讨论】:
-
也许我看错了,但不应该反过来吗? “时间> =时间.当前”?所以你正在测试它是否比现在更大?
-
另外,我知道大写“T”的时间是一个保留字。与小写“t”有冲突吗?使用更具描述性的列名(如“alarm_time”)可能更有意义。
-
Time不是保留字。这是一个类的名称。 -
Rails 有 'DateTime.current',类似于 Ruby 中的 Time.now
标签: ruby-on-rails validation time