Reputation: 166
I'm in section 2.3.3 where we open up the rails console. I'm then able to retrieve the first_user using
first_user = User.first
but I get an error with the next command:
first_user.microposts
Console Message:
> 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>'
Here is what I THINK is happening. I initially tested both the Users and the Microposts by adding, updating, and deleting users and posts. So I think that the micropost is no longer associated with the first user.
But, I don't know how to see which user_id IS associated with the micropost.
I currently have one 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">]
How do I tell which user_id is connected with the micropost?
Upvotes: 0
Views: 232
Reputation: 41
Look at your error messages and the output from Micropost.all
, I think you need to add a new column named user_id
to your microposts
table. In this way, Active Record can know a micropost
belongs to which user
by using the user_id
column. Maybe you forgot to write or run a migration to add user_id
column to microposts
table after you set the relationship between User
model and Micropost
model. You can read this guide to know more about Active Record Associations.
Upvotes: 1
Reputation: 11647
The problem is that your Micropost model has no column named user_id
. Look at your console output from Micropost.all
- there's no attribute in the returned instance named user_id
. Maybe you forgot to run a migration? Maybe you rolled back a migration? Maybe you missed a step completely? When you generated the scaffold, maybe you forgot the part about the user.
Playing around with the app (adding, deleting, editing) won't cause the issue you have. When you retrieve the first user again, and ask to see their miscroposts, if everything was set up properly, you'd get back something - at least an empty relation.
Upvotes: 1