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

2019-09-06 01:54发布

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?

2条回答
Deceive 欺骗
2楼-- · 2019-09-06 02:12

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.

查看更多
霸刀☆藐视天下
3楼-- · 2019-09-06 02:13

solution that works fine for me:

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

u.active = true
u.save
查看更多
登录 后发表回答