rails3无法在模型方法上从NilClass迭代

时间:2013-08-28 16:57:33

标签: ruby-on-rails-3 date methods model null

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

   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) 如果它们是零,则会引发错误“无法从NilClass迭代”

1 个答案:

答案 0 :(得分: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