RoR, Can't iterate from DateTime/TimeWithZone

点点圈 提交于 2019-12-03 09:32:59
start = someModel.start_date.to_datetime
finish = someModel.end_date.to_datetime
while(start < finish) do
  #bunch of awesome stuff
  start += 1.day
end

You must make sure that you are dealing with a Date object (by calling to_date), then everything works as expected:

start_date.to_date.upto(end_date.to_date) {|date| puts date }

Or with a range:

(start_date.to_date..end_date.to_date).to_a

You can't iterate from DateTime. But you can iterate when start and end of interval are instances of Date. Convert them if possible.

And then look at these Date methods:

to use instead of each

If you want to iterate on the range you might consider a loop and step each day using something like someModel.start_date + 1.day.to_i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!