Rails: 遍历 DateTime/TimeWithZone

time_with_zone1..time_with_zone2是无法to_aeach的,可以用while这种方式进行遍历。

例子:

message = Message.order(id: :asc)

first_send_day = message.first.send_at.beginning_of_day
last_send_day = message.last.send_at.beginning_of_day

while first_send_day <= last_send_day do
  message = Message.where('send_at > ? AND send_at < ?', first_send_day, first_send_day.end_of_day)

  ....

  first_send_day += 1.day
end

参考:

RoR, Can’t iterate from DateTime/TimeWithZone