0

我正在定义以下模型方法:

   def live
       deb = start_date||= 100.years.ago # if the start date is nil, it's live
       fin = end_date||=100.years.from_now # if the end date is nil, it's live
       Date.today.between?(deb, fin)
   end

如果填充了 start_date 和 end_date,则该方法返回正确的值(true 或 false),如果它们为 nil,则会引发错误“无法从 NilClass 迭代”

4

1 回答 1

1

也许是不同的方法?

def live
  deb = self.start_date? ? start_date : 100.years.ago
  fin = self.end_date? ? end_date : 100.years.from_now
  Date.today.between?(deb.to_date, fin.to_date)
end
于 2013-08-28T17:08:04.077 回答