ruby-on-rails - 时间戳轨道的自定义验证
问题描述
我有一个小问题。任何人都可以回答如何正确验证此案例?也许需要将时间戳转换为整数,或者其他什么?
型号: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
解决方案
您可以像这样直接比较两个时间戳对象,
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
推荐阅读
- python - nltk 的 wordnet 中的 valueError
- c# - What is the proper way of getting the HTTP request method in an AuthorizationHandler
? - java - Spring KafkaTemplate 在 OpenShift 上找不到引导服务器
- oracle - 错误地与组相关的数据仓库错误
- c# - AspectFill 不适用于 xamarin 中 SkiaSharp 中调整大小的图像,我该怎么办?
- google-apps-script - 如何解决 getrange 错误“范围内的行数必须至少为 1”?
- office365 - 以编程方式将电子邮件从一个 Microsoft 365 用户复制到另一个
- r - R Markdown:使用 pagedown::chrome_print 打印可反应的表会导致一些不可读的行
- sql - 使用多个命名空间从 oracle 中的 CLOB 数据字段中导出 xml 值
- java - 如何更改 HTTPUtlis 中的 api 版本?