Closed baoxin1100 closed 7 years ago
ActiveRecord::RecordInvalid: Validation failed: Email has already been taken
因为用户模型的email是不能重复的(在user model里有相关代码),当你运行了一次seed文件后,再此运行seed会把前一次写进数据库的email再写一遍,因此第二次seed就会报错,只要重复写入就会出错
解决方法:rake db:migrate:reset
,将数据库清空,重新写seed
我也修改了种子文件的代码,重新rake db:seed就会出现这个,,,为什么????
rake aborted!
ActiveRecord::RecordNotSaved: You cannot call create unless the parent is saved
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/associations/collection_association.rb:486:in `_create_record'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/associations/has_many_association.rb:187:in `_create_record'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/associations/collection_association.rb:157:in `create!'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/associations/collection_proxy.rb:306:in `create!'
/home/ubuntu/workspace/db/seeds.rb:141:in `block in <top (required)>'
/home/ubuntu/workspace/db/seeds.rb:129:in `each'
/home/ubuntu/workspace/db/seeds.rb:129:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5.2/lib/active_support/dependencies.rb:268:in `load'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5.2/lib/active_support/dependencies.rb:268:in `block in load'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5.2/lib/active_support/dependencies.rb:240:in `load_dependency'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5.2/lib/active_support/dependencies.rb:268:in `load'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5.2/lib/rails/engine.rb:547:in `load_seed'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/tasks/database_tasks.rb:250:in `load_seed'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/railties/databases.rake:183:in `block (2 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-2.3.0/gems/rake-11.2.2/exe/rake:27:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval'
/usr/local/rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)
提示信息You cannot call create unless the parent is saved
这样的错误一般是因为:你创建了一个新用户user=User.new
,然后你还未执行user.save
操作你就执行了模型关联操作如对这个用户增加一个新的关联课程user.courses.create()
我修改了种子文件的代码,重新rake db:seed就会报错,这是为什么?