简体   繁体   中英

What's the problem with this sentense, i'm getting 'invalid date' error

I am building a timeline chart, so while i am making an array for it, the problem with 'invalid date' appears, maybe i messed up with .map, cause i don't know ruby well

status_and_date = StatusVersion.where(requeat_id:812).pluck(status_id, :date).reverse
@requests_timeline = status_and_date.map do  |r,s| 
  s=s.to_s
  for i in 0..s.length-1

     label = r.to_s

     start_date = s[i].to_date

     end_date = s[i+1].to_date
     [label, start_date, end_date]

     end
  end
  @requests_timeline

Assume that the status_id is incremental by 1. I guess The problem is in your loop, if i == s.length -1 then, s[i+1] will be nil You can use begin ... rescue to catch error to see the detail error by modifying your loop using below code

for i in 0..s.length-1

   begin
     label = r.to_s

     start_date = s[i].to_date

     end_date = s[i+1].to_date
     [label, start_date, end_date]

   rescue
     puts "i - #{i}"
     puts "s[i] - #{s[i]}"
     puts "s[i+1] - #{s[i+1]}"
   end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM