我有一个Active Record模型,它包含两个属性:start_date和end_date.我该如何验证以下内容:
@H_502_2@>日期采用正确的(yyyy-mm-dd)格式
>那个end_date>开始日期
>那个end_date>开始日期
解决方法
它们存储的格式是否重要? Date对象是Date对象.您是将它存储在数据库的日期列中吗?
@H_502_2@以下是我如何进行验证:
class MyModel < ActiveRecord::Base validate :validate_end_date_before_start_date def validate_end_date_before_start_date if end_date && start_date errors.add(:end_date,"Put error text here") if end_date < start_date end end end@H_502_2@请记住,这不会检查无日期…如果可能的话,您可能想要. @H_502_2@仅供参考,如果您希望能够接受各种格式,慢性病非常灵活.