0

我在第 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.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 与微博相关联?

4

2 回答 2

1

查看您的错误消息和输出Micropost.all,我认为您需要在表中添加一个名为的新user_idmicroposts。这样,Active Record 就可以通过该列知道一个micropost属于哪个列。在设置模型和模型之间的关系后,您可能忘记编写或运行迁移以将列添加到表中。您可以阅读本指南以了解有关 Active Record 关联的更多信息。useruser_iduser_idmicropostsUserMicropost

于 2013-06-10T02:45:35.280 回答
1

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

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

于 2013-06-10T02:33:26.373 回答