Problems during rake db:seed, value dosn't set correctly

不打扰是莪最后的温柔 提交于 2019-12-25 01:56:02

问题


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.

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}]) 

but it dosn't store 1. It always stores 0 in database.

also tried this:

User.create([ { :id => 1, :firstname => 'Felix', :lastname => 'Hohlwegler', :active => true}]) 

same problem it stores 0 in db.

Whats going wrong?


回答1:


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
)

passes the validations and creates an user.

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.




回答2:


solution that works fine for me:

u = User.new(
  :id => 1, :firstname => 'Felix', :lastname => 'Hohlwegler', :active => 1
)

u.active = true
u.save


来源:https://stackoverflow.com/questions/29266317/problems-during-rake-dbseed-value-dosnt-set-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!