如何迭代天

时间:2012-11-08 05:49:25

标签: ruby-on-rails ruby loops

我有一个用于循环days.this的代码来制作树叶。我希望列登录和注销将在startdate自动开始并在enddate结束。

exp I input :
startdate: 2012-11-08 01:30:00
enddate: 2012-11-10 01:30:00
i want output like this: 

2012-11-08 01:30:00
2012-11-09 01:30:00
2012-11-010 01:30:00

 for i in 0..((@leafe.enddate - @leafe.startdate).to_i)
                 @attendance = Attendance.new

                 @attendance.signin = '2012-11-08 01:30:00' #value must chang automatically
                 @attendance.signout = '2012-11-08 10:30:00'#value must chang automatically
                 @attendance.user_id = @leafe.user_id
                 @attendance.save
            end

之前感谢

2 个答案:

答案 0 :(得分:0)

如果startdate的类型为Date,为什么不这样做,

@attendance.signin = @leafe.startdate+i
@attendance.signout = @leafe.startdate+i

答案 1 :(得分:0)

为什么使用for循环?它看起来像javacode。 使用以下范围代替块:

(Time.now.beginning_of_day.to_i..Time.now.to_i).step(3600) do |n|#step one hour for example
  @attendance = Attendance.new
  .......
  @attendance.signin  = @leafe.startdate
  @attendance.signout = @leafe.startdate + n # u can change time interval to any what you want

end