Rails不会为夹具生成created_at [英] Rails doesn't generate created_at for fixture
问题描述
我有一个模型问题,其字段为 question_id, elapsed_time, allowed_time和 status,以及一个名为Reporting的控制器,该控制器接收包含问题的JSON并保存。 ( question_id与我的任何模型都不相关)
I have a model Question with "question_id", "elapsed_time", "allowed_time" and "status" as its fields and a controller named Reporting that receive JSON containing questions and save it. ("question_id" is not related to any of my models)
到目前为止,没有问题。直到我尝试运行一些测试。 Rails尝试加载我的问题的装置时出现错误:
ActiveRecord :: StatementInvalid:SQLite3 :: ConstraintException:questions.created_at可能不为NULL:插入问题$ c $ c>
So far there is no problem. Until I try to run some tests. I've an error when Rails is trying to load my Question's fixture :
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
有什么想法吗?
推荐答案
我的模型名称是复数形式。
My model's name was in plural form. Change it to singular and rails will generate timestamp.
查看rails如何生成时间戳: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/fixtures.rb#L560
See how rails generate timestamp : https://github.com/rails/rails/blob/master/activerecord/lib/active_record/fixtures.rb#L560
这篇关于Rails不会为夹具生成created_at的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!