找不到表'用户'

时间:2011-06-14 14:40:33

标签: ruby-on-rails-3

更新4

请参阅下面的解决方案!

**更新3 **

如果有人真的还在读这篇文章(我很感激!)我今天一直在努力解决我的代码问题(是的仍然是一个菜鸟和ruby on rails教程的第11章)。

我应该重置回上一次提交:

$ cd rails_screencast/sample_app/
$ git reset --hard 2396c0d288d132ffc43c82d5cbbc736a5258eed2
HEAD is now at 2396c0d Micropost Validations

当我检查本地主机上的网站时,它实际上显示的是用户列表,而不是ERROR页面,但是当我运行测试套件时(带有spork的自动测试 - 我已经重置了几次以确定)我仍然得到如下所示的所有错误。我对“无法找到表'用户'”非常好奇,因为它出现在每个错误#

  108) Users signin success should sign a user in and out
     Failure/Error: user = Factory(:user)
     ActiveRecord::StatementInvalid:
       Could not find table 'users'
     # ./spec/requests/users_spec.rb:56:in `block (4 levels) in <top (required)>'

Finished in 0.9872 seconds
108 examples, 108 failures

时间继续插入和查看所有这些错误,如果有人可以提供任何见解或提示或想法检查什么我会很感激!或者,如果您需要其他信息,我也可以发布(只是非常具体到哪些文件才能看到,因为我已经没有完全流利的语言,因为很多已经存在)

更新2:

似乎我的重置与出厂设置搞混了,因为有时我的所有错误都指向我的micropost_spec.rb文件的第5行,特别是@user = Factory(:user)行...几乎像我的工厂文件不再与任何内容相关联。我想知道rake db:migrate是否可以解决我的任何问题......或者只是创建新问题...我只有样本数据填写了faker

有什么想法吗?

是否可以将我的文件系统恢复为之前的提交并重新开始? ......当我是绿色时

Failures:

  1) Micropost should create a new instance with valid attributes
     Failure/Error: @user = Factory(:user)
     ActiveRecord::StatementInvalid:
       Could not find table 'users'
     # ./spec/models/micropost_spec.rb:5:in `block (2 levels) in <top (required)>'

  2) Micropost user associations should have a user attribute
     Failure/Error: @user = Factory(:user)
     ActiveRecord::StatementInvalid:
       Could not find table 'users'
     # ./spec/models/micropost_spec.rb:5:in `block (2 levels) in <top (required)>'

  3) Micropost user associations should have the right associated user
     Failure/Error: @user = Factory(:user)
     ActiveRecord::StatementInvalid:
       Could not find table 'users'
     # ./spec/models/micropost_spec.rb:5:in `block (2 levels) in <top (required)>'

  4) Micropost validations should have a user id
     Failure/Error: @user = Factory(:user)
     ActiveRecord::StatementInvalid:
       Could not find table 'users'
     # ./spec/models/micropost_spec.rb:5:in `block (2 levels) in <top (required)>'

  5) Micropost validations should require nonblank content
     Failure/Error: @user = Factory(:user)
     ActiveRecord::StatementInvalid:
       Could not find table 'users'
     # ./spec/models/micropost_spec.rb:5:in `block (2 levels) in <top (required)>'

  6) Micropost validations should reject long content
     Failure/Error: @user = Factory(:user)
     ActiveRecord::StatementInvalid:
       Could not find table 'users'
     # ./spec/models/micropost_spec.rb:5:in `block (2 levels) in <top (required)>'

更新1:

我的错误从4到6到111全部以

的重复主题出现
 1) Micropost should create a new instance with valid attributes
     Failure/Error: @user = Factory(:user)
     ActiveRecord::StatementInvalid:
       Could not find table 'users'
     # ./spec/models/micropost_spec.rb:53:in `block (2 levels) in <top (required)>'

特别是“找不到表'用户'” - 我从教程git中复制了micropost_spec.rb文件中的代码,但似乎没有修复它 - 也许有人可以帮我找到表所指的内容到?

//////////////////////////////////////下面的原文/////// /////////////////////////

我正在使用Ruby on Rails Tutorial第11课,我正在尝试填充我的数据库以显示微博

当我执行rake db:populate命令时,它给了我以下内容:

    macbook:sample_app macbook$ rake db:populate
(in /Users/macbook/rails_screencast/sample_app)
db/test.sqlite3 already exists
db/test.sqlite3 already exists
db/development.sqlite3 already exists
-- create_table("microposts", {:force=>true})
   -> 0.0090s
-- add_index("microposts", ["user_id"], {:name=>"index_microposts_on_user_id"})
   -> 0.0074s
-- create_table("users", {:force=>true})
   -> 0.0243s
-- add_index("users", ["email"], {:name=>"index_users_on_email", :unique=>true})
   -> 0.0094s
-- initialize_schema_migrations_table()
   -> 0.0167s
-- assume_migrated_upto_version(20110614132314, "db/migrate")
   -> 0.0432s
rake aborted!
Validation failed: Email is invalid

我跑了几次并且一直遇到同样的错误......当我在浏览器上访问该网站时,我的样本用户都没有...

require 'faker'


namespace :db do 
  desc "Fill database with sample data"
  task :populate => :environment do
    Rake::Task['db:reset'].invoke
    admin = User.create!(:name => "Foo Bar",
                         :email => "foo@bar.com",
                         :password => "foobar",
                         :password_confirmation => "foobar")
   admin.toggle!(:admin)
    99.times do |n|
      name = Faker::Name.name
      email = "example-#{n+1}@railstutorial.org"
      password = "password"
      User.create!(:name => name,
                   :email => email,
                   :password => password,
                   :password_confirmation => password)
    end

    User.all(:limit => 6).each do |user|
      50.times do
        user.microposts.create!(:content => Faker::Lorem.sentence(5))
      end
    end
  end
end

然后我决定哦也许rake db:reset会清除我的示例数据库,我可以用新鲜的东西再次运行填充...错误的假设

我的测试套件现在显示:

Finished in 0.99915 seconds
111 examples, 111 failures

现在,在我把更多东西弄得乱七八糟之前,我正在寻找一些建议去哪里...

2 个答案:

答案 0 :(得分:118)

Michael(railstutorial.org的作者)回复了我的电子邮件请求!

如果您在运行测试套件时错过了一个表,则可能表明您需要运行:

rake db:test:prepare

YUP !! YAYYYY

Finished in 4.82 seconds
108 examples, 0 failures

蛋糕时间!

答案 1 :(得分:3)

您展示的db:populate本身会db:reset,因此手动执行此操作不会导致db:populate尚未发生任何问题。

您可以删除db/*.sqlite3个文件,然后从新手开始。 db:populate代码中没有任何明显的内容可以解释为什么电子邮件验证失败,您必须显示User模型以及您在那里的验证,以便我们诊断该初始问题。