Rails验证方法比较两个字段?

not*_*ess 15 ruby ruby-on-rails ruby-on-rails-3

我的模型有两个字段,我想作为验证的一部分相互比较.我想确保end_time在start_time之后.我已经编写了一个验证方法来比较它们,但我必须做错了,因为值总是为零.有人可以帮忙吗?

class LogEntry < ActiveRecord::Base
  validates :start_time, :presence => { :message => "must be a valid date/time" }
  validates :end_time, :presence => {:message => "must be a valid date/time"}
  validate :start_must_be_before_end_time

  def start_must_be_before_end_time
    errors.add(:start_time, "must be before end time") unless
       start_time > end_time
  end 
end
Run Code Online (Sandbox Code Playgroud)

得到错误

undefined method `>' for nil:NilClass
Run Code Online (Sandbox Code Playgroud)

所以,start_time和/或end_time都是零.我以为我跟着我发现的很多例子,但显然不是.我错过了什么?

谢谢.

bra*_*rad 16

我最好的猜测是你需要你的方法看起来像这样:

private

def start_must_be_before_end_time
    errors.add(:start_time, "must be before end time") unless
        start_time < end_time
end 
Run Code Online (Sandbox Code Playgroud)

(另外,注意<而不是>(或改为if>=)

如果这不起作用,那么你也应该检查start_timeend_time在控制器中正确定义,因为如果跨多个表单元素创建时间,可能会发生有趣的事情.


Mar*_*rth 5

您需要自己检查存在(如果不存在,则跳过验证步骤).

def start_must_be_before_end_time
  return unless start_time and end_time
  errors.add(:start_time, "must be before end time") unless start_time < end_time
end
Run Code Online (Sandbox Code Playgroud)

打印"必须是有效日期/时间"或"开始时间必须在结束时间之前".

替代

def start_must_be_before_end_time
  valid = start_time && end_time && start_time < end_time
  errors.add(:start_time, "must be before end time") unless valid
end
Run Code Online (Sandbox Code Playgroud)

如果未设置start_time或end_time,则打印"开始时间必须是有效日期/时间"和"开始时间必须在结束时间之前".

对于个人喜好第一,因为它只显示了用户做错了.后者就像许多网站一样只是将20行错误文本加载到用户身上,因为程序员认为看到每个验证结果都会很好.糟糕的UX.