picardo
picardo

Reputation: 24886

Rails cannot write to database field

I am trying to do a very simple update on a field that does not have any validation whatsoever. However, the update always fails. Here is what the code looks like:

# model
class Event < ActiveRecord::Base

  attr_accessible :start_time

  ..

end

# migration
class CreateEvents < ActiveRecord::Migration
  def change
    t.datetime :start_time
  end
end

# console
Event.first.update_attribute(:start_time, "02:00")

The query that was run in the Rails log does not even include the start_time attribute!

(0.2ms) BEGIN

(4.5ms) UPDATE events SET updated_at = '2012-07-24 19:51:33', repeat_days = '--- \n- wed\n- sat\n- sun\n', event_date_list = '--- []\n\n' WHERE events.id = 3763

(5.5ms) COMMIT

I cannot begin to make sense of this. Can anyone help me understand the root cause of this problem?

Upvotes: 0

Views: 101

Answers (1)

MrDanA
MrDanA

Reputation: 11647

You are a passing it a string, not a Date, Time, or Datetime object.

It looks like you just want to store the time, not the date attached. But maybe you meant to attach a date as well. If you want to store the date as well, look up the Datetime class.

If you want to store just the time (hours, minutes, and seconds), then I would suggest you change your start_time field to be an integer, and store the seconds: 2.hours or 2.hours + 4.minutes + 6.seconds.

You can convert that easily in to time again.

Upvotes: 2

Related Questions