【发布时间】:2016-06-30 22:29:58
【问题描述】:
在表单中,用户必须选择月份和年份。我想确保日期不是基于月份的未来。因此,假设当前日期是 07/01/2016,在这种情况下,用户应该能够选择 07/2016,但不能选择 08/2016。
验证似乎有效,但看起来不太好。有没有更简单的方法 实现这个?
validate :founded_in_the_past
def founded_in_the_past
if founded && ((founded.month > Date.current.month && founded.year == Date.current.year)
&& founded.year > Date.current.year)
errors.add :base, "Founding date must be in the past."
end
end
【问题讨论】:
-
该条件看起来总是错误的:
founded.year == Date.current.year和founded.year > Date.current.year。我假设您的意思是最后一个&&是||? -
潜伏者你是对的。最后一个应该是
||
标签: ruby-on-rails validation date activerecord