本文介绍了RoR,无法从DateTime / TimeWithZone进行迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的任务,我想取一个开始日期和一个结束日期,然后遍历天/日期。我的db:seed rake任务中使用了此代码。目前,我的代码经过以下尝试。

I have a simple task where I want to take a starting date and an ending date and loop over the days/dates. This code is being used in my db:seed rake task. Currently, my code has gone through the following attempts.

(someModel.start_date.to_datetime..someModel.end_date.to_datetime).each { 
    |x| puts x 
}
 ......
(someModel.start_date...someModel.end_date).each { |x| puts x }

在每种情况下,我都会收到这样的错误。

In each case, I get an error like this.

can't iterate from ActiveSupport::TimeWithZone
or 
can't iterate from DateTime

如果有人对如何在一系列DateTime上进行迭代有任何了解,我将非常感激。

If anyone has any clue on how to iterate over a range of DateTimes I'd be greatly appreciative.

推荐答案

start = someModel.start_date.to_datetime
finish = someModel.end_date.to_datetime
while(start < finish) do
  #bunch of awesome stuff
  start += 1.day
end

这篇关于RoR,无法从DateTime / TimeWithZone进行迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 15:12