2

I'm using a task and the spreadsheet gem to read in an excel spreadsheet into my database. One of the columns I'm reading in is "start_time." To do this, I'm forming an array of values, then passing in each of these array values, one by one.

  cnum_array = [] # for start times
  sheet1.each 3 do |row|
    unless row[9].blank?
      time = Time.parse(row[9])
      cnum_array << time.utc
    end 
  end

  count = 0
  for course in Course.all
    course.update_attribute :start_time, cnum_array[count]
    count += 1
  end

This seems to work fine. If I insert a "puts course.start_time" statement within this last loop, it prints off the right time. Like so:

  count = 0
  for course in Course.all
    course.update_attribute :start_time, cnum_array[count]
    puts course.start_time
    count += 1
  end

This gives me the right time, e.g. "2012-01-23 15:30:00."

But when I look up the course time later (e.g. via my console's Course.find(1).start_time), it gives me "2000-01-01 15:20:00." So the time of day is right, but the day itself goes back to 2000-01-01.

Does anyone know why this is happening, and how I can fix it? Thanks!

jyli7
  • 2,691
  • 5
  • 21
  • 31

1 Answers1

8

You are using the Time class. This class deals with times, not dates. My guess is that your database column is of type time as well.

I recommend you use a datetime (or possibly timestamp) column type.

rdvdijk
  • 4,360
  • 25
  • 29