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