问题描述
我想用 app/db 中的 seed.rb 文件预填充我的表
I want to prefill my table with the seeds.rb file in app/db
但是我在存储正确数据时遇到了问题.
but there I got a problem during storing the right data.
在用户表中,我有一个名为 active 的列,其数据类型为 tinyint.所以现在我想用seeds.rb int值存储
in the table users I have an column called active withe datatype tinyint. so now I want to store with the seeds.rb int values
User.create([ { :id => 1, :firstname => 'Felix', :lastname => 'Hohlwegler', :active => 1}])
但它不存储 1.它总是在数据库中存储 0.
but it dosn't store 1. It always stores 0 in database.
也试过这个:
User.create([ { :id => 1, :firstname => 'Felix', :lastname => 'Hohlwegler', :active => true}])
同样的问题,它在 db 中存储 0.
same problem it stores 0 in db.
怎么了?
推荐答案
导致此类问题的最常见的事情是验证.在这种情况下,我希望用户需要电子邮件或密码.请检查是否
The most common thing that causes this kind of problems are validations. In this case I would expect that a user needs an email or a password. Please check if
User.create!(
:id => 1, :firstname => 'Felix', :lastname => 'Hohlwegler', :active => true
)
通过验证并创建用户.
请注意,我使用 create!
如果它无法创建用户而不是仅返回未保存的用户,则会引发错误.
Please note that I use create!
that would raise an error if it not able to create an user instead of just returning an unsaved one.
这篇关于rake db:seed 期间的问题,值设置不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!