ActiveRecord doesn't set a datetime field
I have a datetime field called time in my MotionRecord model. I try to set it using this command:
MotionRecord.create({:time=> "2010-10-15 15:10:24", :chart_id=>1})
Oddly enough this results in the following input:
<MotionRecord开发者_运维技巧 id: 1, time: nil, chart_id: 1>
I'm not sure what I am doing wrong.
Edit: This is my model.
class MotionRecord < ActiveRecord::Base
belongs_to :chart
belongs_to :activity
attr_accessor :time
end
and my schema
create_table :motion_records do |t|
t.datetime :time
t.integer :chart_id
t.integer :activity_id
t.timestamps
end
This is related to your other question. Use attr_accessible :time
and not attr_accessor
. See "WARNING: Can't mass-assign protected attributes"
Or you can just set it after the create using update_attribute(:field, "value")
精彩评论