gpt4 book ai didi

ruby-on-rails - Ruby on Rails 中的种子文件错误

转载 作者:数据小太阳 更新时间:2023-10-29 07:52:57 25 4
gpt4 key购买 nike

我有一个种子文件,用于将一些测试数据植入我的系统。

fnames = ["Adam", "Benjamin", "Caleb", "Daniel", "Frank", "Gideon"]
lnames = ["Smith", "Jones", "Washington", "Jefferson", "Gardener",
"Cooper"]
Student.delete_all
3.times do
nickname = fnames[rand(6)]
name = nickname + " " + lnames[rand(6)]
email = (name + "@foobar.com").gsub(" ", "")
Student.create(name: name, email: email, nickname: nickname)
end

Task.delete_all
task_list = [
[ "First", 540 ],
[ "Second", 850 ],
[ "Third", 905 ],
[ "Fourth", 80000 ]
]

Work.delete_all

task_list.each do |description, points|
work = Work.create
task = Task.create( description: description, points: points )
student = Student.first
student.works << work
student.save
work.task = task
work.save
end

但是如果我运行它然后我得到这个错误:

rails@rails-VirtualBox:~/rails_projects/rails-for-business$ rake db:seed
[RailsAdmin] RailsAdmin initialization disabled by default. Pass SKIP_RAILS_ADMIN_INITIALIZER=false if you need it.
rake aborted!
undefined method `works' for nil:NilClass
/home/rails/rails_projects/rails-for-business/db/seeds.rb:34:in `block in <top (required)>'
/home/rails/rails_projects/rails-for-business/db/seeds.rb:30:in `each'
/home/rails/rails_projects/rails-for-business/db/seeds.rb:30:in `<top (required)>'
/home/rails/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:222:in `load'
/home/rails/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:222:in `block in load'
/home/rails/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:213:in `load_dependency'
/home/rails/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:222:in `load'
/home/rails/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/railties-4.0.0/lib/rails/engine.rb:540:in `load_seed'
/home/rails/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/activerecord-4.0.0/lib/active_record/tasks/database_tasks.rb:153:in `load_seed'
/home/rails/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/activerecord-4.0.0/lib/active_record/railties/databases.rake:181:in `block (2 levels) in <top (required)>'
/home/rails/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/bin/ruby_executable_hooks:15:in `eval'
/home/rails/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)

我找不到其中的错误。由于这是一个协作项目,我不知道数据库本身中的表是如何连接的。

最佳答案

您的Student 记录似乎没有创建(因此,Student.first 返回nil)。我猜你应该检查验证错误。

关于ruby-on-rails - Ruby on Rails 中的种子文件错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20098398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com