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

Ruby

John Murphy
John Murphy
1,122 Points

Creating the Friendship Errors - "AssociationTypeMismatch"

I've been cracking through the Treebook videos and I've now hit a wall. I am on the "Building Social Features in Ruby on Rails" section, on the 5th Video - "Creating the Friendship" and the tests are failing with 5 errors which indicate an AssociationTypeMismatch.

I have updated my code on github and would REALLY appreciate somebody to have a look through and see if where I am going wrong as I am using this tutorial as part of a larger project and would hate to have to start all over again.

(My project is called Clonyn - not Treebook, ignore anything thats not related to Treebook :) )

https://github.com/woodyeire/clonyn

Below are the test errors as copied from my terminal.

woodyeire@clonyn:~/workspace/clonyn (master) $ ruby -I test test/functional/user_friendships_controller_test.rb 
/usr/local/rvm/gems/ruby-2.2.1@global/gems/activesupport-3.2.18/lib/active_support/values/time_zone.rb:270: warning: circular argument reference - now
Loaded suite test/functional/user_friendships_controller_test
Started
/usr/local/rvm/gems/ruby-2.2.1@global/gems/activerecord-3.2.18/lib/active_record/associations/has_many_association.rb:53: warning: circular argument reference - reflection
/usr/local/rvm/gems/ruby-2.2.1@global/gems/activerecord-3.2.18/lib/active_record/associations/has_many_association.rb:57: warning: circular argument reference - reflection
/usr/local/rvm/gems/ruby-2.2.1@global/gems/activerecord-3.2.18/lib/active_record/associations/has_many_association.rb:61: warning: circular argument reference - reflection
/usr/local/rvm/gems/ruby-2.2.1@global/gems/activerecord-3.2.18/lib/active_record/associations/has_many_association.rb:80: warning: circular argument reference - reflection
E
===============================================================================
Error: test: #create when logged in with a valid friend_id should Test - - - . (UserFriendshipsControllerTest)
: ActiveRecord::AssociationTypeMismatch: User(#23020660) expected, got ActiveRecord::Relation(#31440640)
app/controllers/user_friendships_controller.rb:19:in `create'
test/functional/user_friendships_controller_test.rb:98:in `block (4 levels) in <class:UserFriendshipsControllerTest>'
===============================================================================
E
===============================================================================
Error: test: #create when logged in with a valid friend_id should assign a friend object. (UserFriendshipsControllerTest)
: ActiveRecord::AssociationTypeMismatch: User(#23020660) expected, got ActiveRecord::Relation(#31440640)
app/controllers/user_friendships_controller.rb:19:in `create'
test/functional/user_friendships_controller_test.rb:98:in `block (4 levels) in <class:UserFriendshipsControllerTest>'
===============================================================================
E
===============================================================================
Error: test: #create when logged in with a valid friend_id should assign a user_friendship object. (UserFriendshipsControllerTest)
: ActiveRecord::AssociationTypeMismatch: User(#23020660) expected, got ActiveRecord::Relation(#31440640)
app/controllers/user_friendships_controller.rb:19:in `create'
test/functional/user_friendships_controller_test.rb:98:in `block (4 levels) in <class:UserFriendshipsControllerTest>'
===============================================================================
E
===============================================================================
Error: test: #create when logged in with a valid friend_id should create a friendship. (UserFriendshipsControllerTest)
: ActiveRecord::AssociationTypeMismatch: User(#23020660) expected, got ActiveRecord::Relation(#31440640)
app/controllers/user_friendships_controller.rb:19:in `create'
test/functional/user_friendships_controller_test.rb:98:in `block (4 levels) in <class:UserFriendshipsControllerTest>'
===============================================================================
E
===============================================================================
Error: test: #create when logged in with a valid friend_id should redirect to the profile page of the friend. (UserFriendshipsControllerTest)
: ActiveRecord::AssociationTypeMismatch: User(#23020660) expected, got ActiveRecord::Relation(#31440640)
app/controllers/user_friendships_controller.rb:19:in `create'
test/functional/user_friendships_controller_test.rb:98:in `block (4 levels) in <class:UserFriendshipsControllerTest>'
===============================================================================
............

Finished in 0.207162187 seconds.

17 tests, 13 assertions, 0 failures, 5 errors, 0 pendings, 0 omissions, 0 notifications
0% passed

82.06 tests/s, 62.75 assertions/s

1 Answer

Raymond Sapida
Raymond Sapida
33,049 Points

Hi there,

In my experience, most of the errors related to AssociationTypeMismatch has something to do with how the params are being called in the controller or the test . It might be because of line 18 of your user friendship controller. Your code has :

@friend = User.where(profile_name: params[:user_friendship][:friend_id].first)

when I believe it should be:

@friend = User.where(profile_name: params[:user_friendship][:friend_id]).first

I hope that helps!

John Murphy
John Murphy
1,122 Points

You, my friend, are a beautiful person. That was it :) All tests passing now!

Thank you so much!