Rails不会为fixture创建created_at [英] Rails don't generate created_at for fixture

查看:110
本文介绍了Rails不会为fixture创建created_at的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型问题与question_id,elapsed_time,allowed_time和状态作为其字段和一个名为Reporting的控制器接收含有问题的JSON并保存。 (question_id与我的任何模型无关)



到目前为止没有问题。直到我尝试运行一些测试。我有一个错误,当Rails试图加载我的问题的夹具:
ActiveRecord :: StatementInvalid:SQLite3 :: ConstraintException:questions.created_at可能不是NULL:INSERT INTO问题



这是我的装置:

  
id:1
question_id:MyString
应用程序:MyString
guid:1
状态: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:sent_at
t.integer:status

t.timestamps
end
end
end
任何想法?

解决方案

我的模型名称是复数形式。将其更改为单数,rails将生成时间戳。



查看rails如何生成时间戳: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/fixtures.rb#L560


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)

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"

Here's my fixture :

one:
  id: 1
  question_id: MyString
  app: MyString
  guid: 1
  status: 1
  elapsed_time: 1
  allowed_time: 1

and my model :

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

Any idea ?

解决方案

My model's name was in plural form. Change it to singular and rails will generate timestamp.

See how rails generate timestamp : https://github.com/rails/rails/blob/master/activerecord/lib/active_record/fixtures.rb#L560

这篇关于Rails不会为fixture创建created_at的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆