Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Gabe Med
7,457 Pointsdon't understand the meaning of these 2 errors when testing user_friendship_test.rb
in the setting up the state machine video tutorial, I did all the testing but can't seem to clear these two errors, please help. here are the errors and i don't know exactly what they mean.
1) Error:
test: #accept! should include the friend in the list of friends . (UserFriendshipTest):
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: user_friendships.state: SELECT "users".* FROM "users" INNER JOIN "user_friendships" ON "users"."id" = "user_friendships"."friend_id" WHERE "user_friendships"."user_id" = 475529081 AND ("user_friendships"."state" = 'accepted')
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:91:in initialize'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:91:innew'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:91:in prepare'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/sqlite_adapter.rb:246:inblock in exec_query'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_adapter.rb:280:in block in log'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/notifications/instrumenter.rb:20:ininstrument'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_adapter.rb:275:in log'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/sqlite_adapter.rb:242:inexec_query'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/sqlite_adapter.rb:467:in select'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/database_statements.rb:18:inselect_all'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in select_all'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/querying.rb:38:inblock in find_by_sql'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/explain.rb:40:in logging_query_plan'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/querying.rb:37:infind_by_sql'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/relation.rb:171:in exec_queries'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/relation.rb:160:inblock in to_a'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/explain.rb:40:in logging_query_plan'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/relation.rb:159:into_a'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/relation/finder_methods.rb:159:in all'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/associations/has_many_through_association.rb:173:infind_target'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/associations/collection_association.rb:333:in load_target'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/associations/association.rb:55:inreload'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/associations/collection_proxy.rb:126:in reload'
test/unit/user_friendship_test.rb:58:inblock (2 levels) in <class:UserFriendshipTest>'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/shoulda-context-1.0.2/lib/shoulda/context/context.rb:398:in call'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/shoulda-context-1.0.2/lib/shoulda/context/context.rb:398:inblock in create_test_from_should_hash'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/mocha-0.10.5/lib/mocha/integration/mini_test/version_230_to_262.rb:28:in `run'
2) Error:
test_that_creating_a_friendship_based_on_user_id_and_friend_id_works(UserFriendshipTest):
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: user_friendships.state: SELECT 1 AS one FROM "users" INNER JOIN "user_friendships" ON "users"."id" = "user_friendships"."friend_id" WHERE "user_friendships"."user_id" = 475529081 AND "user_friendships"."state" = 'pending' AND "users"."id" = 615722309 LIMIT 1
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:91:in initialize'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:91:innew'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:91:in prepare'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/sqlite_adapter.rb:246:inblock in exec_query'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_adapter.rb:280:in block in log'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/notifications/instrumenter.rb:20:ininstrument'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_adapter.rb:275:in log'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/sqlite_adapter.rb:242:inexec_query'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/sqlite_adapter.rb:467:in select'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/database_statements.rb:18:inselect_all'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in select_all'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/database_statements.rb:24:inselect_one'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/database_statements.rb:30:in select_value'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/relation/finder_methods.rb:202:inexists?'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/associations/collection_association.rb:324:in include?'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/associations/collection_proxy.rb:46:ininclude?'
test/unit/user_friendship_test.rb:15:in block in <class:UserFriendshipTest>'
/usr/local/rvm/gems/ruby-1.9.3-p194/gems/mocha-0.10.5/lib/mocha/integration/mini_test/version_230_to_262.rb:28:inrun'
9 tests, 7 assertions, 0 failures, 2 errors, 0 skips
6 Answers
Benjamin Bean
2,140 PointsMaybe rake db:test:prepare?
Gabe Med
7,457 Pointssolved my problem. I needed to delete and re- generate a migration file, run rake and migrate the db and THEN finally do rake db:test:prepare
Thanks for the pointer Benjamin!
Brad Halstead
5,662 PointsI had this same exact error. Both above comments led me to what was going on. I ended up deleting and regenerating the migration file... I copied my code (add_column... & add_index...) from add_state_to_user_friendships.rb file before deleting and pasted to a newly generated add_state_to_user_friendship.rb file. Then I tried running rake db:migrate and an error occured, leading me to what was likely my problem the entire time, a typo in the code written in the file. I searched high and low for a typo when I got the exact same 2 errors as Gabe Med posted above, but I was looking in user_friendship_test on lines 15 and 58... typo was in add_index... ugh! Test errors were pointing me in the wrong direction looking for a typo... lol Sorry for long message, just wanted to save anyone else the time if they got this same error.
Lastly ran rake db:migrate, then rake db:test:prepare, then the user_friendships_test... and volla, no issues.
Alejandro Cárdenas
4,777 PointsHey, I have the exact same problem. I've checked for typos in the migration file, in the user_frienship.rb, and user.rb model, but there aren't any. I've even tried deleting the db with rake db:drop, and then creating it all over again with db:migrate, preparing the db for testing with rake db:test:prepare, but still no success, I get this error:
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: user_friendship.state: SELECT "users".* FROM "users" INNER JOIN "user_friendships" ON "users"."id" = "user_friendships"."friend_id" WHERE "user_friendships"."user_id" = 765506074 AND ("user_friendship"."state" = 'accepted')
This is my test code:
context '#accept!' do
setup { @user_friendship = UserFriendship.create user: users(:acib708), friend: users(:bo) }
should 'set state to accepted' do
@user_friendship.accept!
assert_equal 'accepted', @user_friendship.state
end
should 'send acceptance email' do
assert_difference('ActionMailer::Base.deliveries.size', 1) { @user_friendship.accept! }
end
should 'include friend in list of friends' do
@user_friendship.accept!
users(:acib708).friends.reload
assert users(:acib708).friends.include? users :bo
end
end
The error in the console says it fails in the 'users(:acib708).friends.reload' line. I tried 'puts users(:acib708).friends.inspect' and it also fails.
Any ideas? :(
Alejandro Cárdenas
4,777 PointsASDOGINDBUDOABJHVDTLIUBGTPIABV! Fixed it. I'll hang myself haha, I ended up reading everything about databases in rails, migrated everything to PostgreSQL, only to find out my mistake was in user.rb When adding the conditions argument to the has_many :friends line, I put
conditions: { user_friendship: { state: :accepted } }
instead of
conditions: { user_friendships: { state: :accepted } }
I understand that user_friendships is making reference to a table inside the DB, but doesn't rails handle that plural words thing? Anyway, hope this never happens to anyone. Cheers, Alex.
Nathan F.
30,773 PointsFor those who experience this error, as I also did, it came down to a peculiar struggle I've been having throughout revisiting this course, though it wasn't at issue when I first began. In migrating and preparing the database, I have found it necessary to specify a test environment for my tests to run properly, like so:
rake db:migrate RAILS_ENV=test
rake db:test:prepare RAILS_ENV=test
I then saw the migrations made and got the proper failure message.