首页 > 解决方案 > 时间戳轨道的自定义验证

问题描述

我有一个小问题。任何人都可以回答如何正确验证此案例?也许需要将时间戳转换为整数,或者其他什么?

型号:unworked_day.rb

class UnworkedDay < ApplicationRecord
  validate :end_date_after_start_date?

  def end_date_after_start_date?
    if unworked_period_to.to_i < unworked_period_from.to_i
       errors.add :unworked_period_to, "Unworked period to should be greater than the unworked period from"
    end
  end

移民

  t.timestamp :unworked_period_from
  t.timestamp :unworked_period_to

标签: ruby-on-railsruby

解决方案


您可以像这样直接比较两个时间戳对象,

  def end_date_after_start_date?
    if unworked_period_to < unworked_period_from
       errors.add :unworked_period_to, "Unworked period to should be greater than the unworked period from"
    end
  end

无需转换为任何其他 data_type


推荐阅读