RoR,无法从DateTime/TimeWithZone迭代

Bla*_*ite 25 ruby datetime ruby-on-rails seeding

我有一个简单的任务,我想要开始日期和结束日期并循环日期/日期.此代码正在我的db:seed rake任务中使用.目前,我的代码经历了以下尝试.

(someModel.start_date.to_datetime..someModel.end_date.to_datetime).each { 
    |x| puts x 
}
 ......
(someModel.start_date...someModel.end_date).each { |x| puts x }
Run Code Online (Sandbox Code Playgroud)

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

can't iterate from ActiveSupport::TimeWithZone
or 
can't iterate from DateTime
Run Code Online (Sandbox Code Playgroud)

如果有人知道如何迭代一系列DateTimes,我将非常感激.

Chr*_*tto 23

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


col*_*rco 9

你必须确保你正在处理一个Date对象(通过调用to_date),然后一切都按预期工作:

start_date.to_date.upto(end_date.to_date) {|date| puts date }
Run Code Online (Sandbox Code Playgroud)

或者有一个范围:

(start_date.to_date..end_date.to_date).to_a
Run Code Online (Sandbox Code Playgroud)