4

我的代码如下所示:

if f.thisyear.blank? &&  Date.new(Date.today.year, f.birthday.month, f.birthday.mday ).valid_date?
  f.update_attribute :thisyear, Date.new(Date.today.year, f.birthday.month, f.birthday.mday )
end

在第一行中,我使用以下命令检查有效日期:

Date.new(Date.today.year, f.birthday.month, f.birthday.mday ).valid_date?

这会引发错误:

`new': invalid date (ArgumentError)

我如何编写代码来避免这个问题?

4

1 回答 1

13

Date::valid_date?是一个方法,所以不是

Date.new(Date.today.year, f.birthday.month, f.birthday.mday).valid_date?

采用

Date.valid_date?(Date.today.year, f.birthday.month, f.birthday.mday)
于 2013-09-19T20:25:14.407 回答