RoR, Can't iterate from DateTime/TimeWithZone RoR, Can't iterate from DateTime/TimeWithZone ruby-on-rails ruby-on-rails

RoR, Can't iterate from DateTime/TimeWithZone


start = someModel.start_date.to_datetimefinish = someModel.end_date.to_datetimewhile(start < finish) do  #bunch of awesome stuff  start += 1.dayend


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 ofinterval are instances of Date. Convert them if possible.

And then look at these Date methods:

to use instead of each