Rails教程第2章,第2.3.3节

时间:2013-06-10 02:24:44

标签: ruby-on-rails railstutorial.org

我在第2.3.3节中打开了rails控制台。 然后我可以使用

检索first_user
first_user = User.first

但是我在下一个命令时遇到错误:

first_user.microposts

控制台消息:

> first_user.microposts
Micropost Load (0.4ms)  SELECT "microposts".* FROM "microposts" WHERE "microposts"."user_id" = 1
    ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: microposts.user_id: SELECT "microposts".* FROM "microposts"  WHERE "microposts"."user_id" = 1
        from /Users/ryentzer/.rvm/gems/ruby-1.9.3-p429@rails3tutorial2ndEd/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:91:in `initialize'
        from /Users/ryentzer/.rvm/gems/ruby-1.9.3-p429@rails3tutorial2ndEd/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:91:in `new'
        from /Users/ryentzer/.rvm/gems/ruby-1.9.3-p429@rails3tutorial2ndEd/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:91:in `prepare'
        from /Users/ryentzer/.rvm/gems/ruby-1.9.3-p429@rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/connection_adapters/sqlite_adapter.rb:246:in `block in exec_query'
        from /Users/ryentzer/.rvm/gems/ruby-1.9.3-p429@rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
        from /Users/ryentzer/.rvm/gems/ruby-1.9.3-p429@rails3tutorial2ndEd/gems/activesupport-3.2.13/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
        from /Users/ryentzer/.rvm/gems/ruby-1.9.3-p429@rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
        from /Users/ryentzer/.rvm/gems/ruby-1.9.3-p429@rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/connection_adapters/sqlite_adapter.rb:242:in `exec_query'
        from /Users/ryentzer/.rvm/gems/ruby-1.9.3-p429@rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/connection_adapters/sqlite_adapter.rb:467:in `select'
        from /Users/ryentzer/.rvm/gems/ruby-1.9.3-p429@rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/database_statements.rb:18:in `select_all'
        from /Users/ryentzer/.rvm/gems/ruby-1.9.3-p429@rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
        from /Users/ryentzer/.rvm/gems/ruby-1.9.3-p429@rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/querying.rb:38:in `block in find_by_sql'
        from /Users/ryentzer/.rvm/gems/ruby-1.9.3-p429@rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/explain.rb:41:in `logging_query_plan'
        from /Users/ryentzer/.rvm/gems/ruby-1.9.3-p429@rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/querying.rb:37:in `find_by_sql'
        from /Users/ryentzer/.rvm/gems/ruby-1.9.3-p429@rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/relation.rb:171:in `exec_queries'
        from /Users/ryentzer/.rvm/gems/ruby-1.9.3-p429@rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/relation.rb:160:in `block in to_a'
        from /Users/ryentzer/.rvm/gems/ruby-1.9.3-p429@rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/explain.rb:34:in `logging_query_plan'
        from /Users/ryentzer/.rvm/gems/ruby-1.9.3-p429@rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/relation.rb:159:in `to_a'
        from /Users/ryentzer/.rvm/gems/ruby-1.9.3-p429@rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/relation/finder_methods.rb:159:in `all'
        from /Users/ryentzer/.rvm/gems/ruby-1.9.3-p429@rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/associations/collection_association.rb:382:in `find_target'
        from /Users/ryentzer/.rvm/gems/ruby-1.9.3-p429@rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/associations/collection_association.rb:335:in `load_target'
        from /Users/ryentzer/.rvm/gems/ruby-1.9.3-p429@rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/associations/collection_proxy.rb:44:in `load_target'
        from /Users/ryentzer/.rvm/gems/ruby-1.9.3-p429@rails3tutorial2ndEd/gems/activerecord-3.2.13/lib/active_record/associations/collection_proxy.rb:87:in `method_missing'
        from /Users/ryentzer/.rvm/gems/ruby-1.9.3-p429@rails3tutorial2ndEd/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
        from /Users/ryentzer/.rvm/gems/ruby-1.9.3-p429@rails3tutorial2ndEd/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
        from /Users/ryentzer/.rvm/gems/ruby-1.9.3-p429@rails3tutorial2ndEd/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'

以下是我认为正在发生的事情。我最初通过添加,更新和删除用户和帖子来测试用户和微博。所以我认为微博不再与第一个用户相关联。

但是,我不知道如何来查看哪个user_id与微博相关联。

我目前有一个Micropost:

> Micropost.all
Micropost Load (0.3ms)  SELECT "microposts".* FROM "microposts" => [#<Micropost id: 4, content: "Testing", created_at: "2013-06-10 01:24:34", updated_at: "2013-06-10 01:24:34">] 

如何判断哪个user_id与微博连接?

2 个答案:

答案 0 :(得分:1)

问题是您的Micropost模型没有名为user_id的列。查看Micropost.all的控制台输出 - 返回的实例中没有名为user_id的属性。也许你忘记了进行迁移?也许你回滚了迁移?也许你完全错过了一步?当您生成脚手架时,可能您忘记了有关用户的部分。

使用应用程序(添加,删除,编辑)不会导致您遇到问题。当您再次检索第一个用户并要求查看他们的错误信息时,如果一切设置正确,您将收到一些东西 - 至少是空关系。

答案 1 :(得分:1)

查看您的错误消息和Micropost.all的输出,我认为您需要在user_id表中添加一个名为microposts的新列。通过这种方式,Active Record可以使用micropost列知道user属于哪个user_id。在设置user_id模型和microposts模型之间的关系后,您可能忘记编写或运行迁移以将User列添加到Micropost表。您可以阅读this guide以了解有关Active Record Association的更多信息。