我有一个模型问题,其中包含“question_id”,“elapsed_time”,“allowed_time”和“status”作为其字段,以及一个名为Reporting的控制器,它接收包含问题的JSON并保存。 (“question_id”与我的任何模型无关)
到目前为止没有问题。直到我尝试运行一些测试。当Rails尝试加载我的问题夹具时,我出错了:
ActiveRecord::StatementInvalid: SQLite3::ConstraintException: questions.created_at may not be NULL: INSERT INTO "questions
“
这是我的装备:
one:
id: 1
question_id: MyString
app: MyString
guid: 1
status: 1
elapsed_time: 1
allowed_time: 1
和我的模特:
class CreateQuestions < ActiveRecord::Migration
def change
create_table :questions do |t|
t.string :app
t.integer :guid
t.string :question_id
t.integer :elapsed_time
t.integer :allowed_time
t.datetime :happened_at
t.integer :status
t.timestamps
end
end
end
有什么想法吗?
答案 0 :(得分:2)
如https://stackoverflow.com/a/4350202/978525中所述,您可以在夹具对象中添加以下内容:
objectA:
foo: something
created_at: <%= 5.day.ago.to_s(:db) %>
updated_at: <%= 5.day.ago.to_s(:db) %>
答案 1 :(得分:1)
我的模特的名字是复数形式。将其更改为单数,rails将生成时间戳。
了解rails如何生成时间戳:https://github.com/rails/rails/blob/master/activerecord/lib/active_record/fixtures.rb#L560