# Unknown column 'friendships.id'?

**URL:** <https://www.ruby-forum.com/t/unknown-column-friendships-id/130458>\
**Category:** Rails\
**Created:** [February 28, 2008, 1:19pm UTC](https://www.ruby-forum.com/t/unknown-column-friendships-id/130458 "2008-02-28T13:19:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kaizerkaizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/45deac/32.png) [@kaizerkaizer](https://www.ruby-forum.com/u/kaizerkaizer)\
**Post date:** [February 28, 2008, 1:19pm UTC](https://www.ruby-forum.com/t/unknown-column-friendships-id/130458/1 "2008-02-28T13:19:48Z")

</div>

I can’t understand where it fails. I can add a friend to my friendship  
table, but I want to view my friends which I have after that. Rendering  
the view of this (index) fails.

I understand that I somewhere refer to my friendship wrongly, but it  
seems to find everything else. I can only find a referance in this log  
in the last row, but that tells me (like the noob I am) nothing. Someone  
got a suggestion (preferably a solution)?

############################################  
statement invalidin FriendsController#index  
############################################  
ActiveRecord::StatementInvalid (Mysql::Error: #42S22Unknown column  
‘friendships.id’ in ‘field list’: SELECT users.`id` AS t0\_r0,  
users.`login` AS t0\_r1, users.`email` AS t0\_r2, users.`crypted_password`  
AS t0\_r3, users.`salt` AS t0\_r4, users.`created_at` AS t0\_r5,  
users.`updated_at` AS t0\_r6, users.`remember_token` AS t0\_r7,  
users.`remember_token_expires_at` AS t0\_r8, users.`l_name` AS t0\_r9,  
users.`f_name` AS t0\_r10, users.`last_activity` AS t0\_r11,  
users.`last_activity_at` AS t0\_r12, friendships.`id` AS t1\_r0,  
friendships.`user_id` AS t1\_r1, friendships.`friend_id` AS t1\_r2,  
friendships.`xfn_friend` AS t1\_r3, friendships.`xfn_acquaintance` AS  
t1\_r4, friendships.`xfn_contact` AS t1\_r5, friendships.`xfn_met` AS  
t1\_r6, friendships.`xfn_coworker` AS t1\_r7, friendships.`xfn_colleague`  
AS t1\_r8, friendships.`xfn_classmate` AS t1\_r9,  
friendships.`xfn_neighbor` AS t1\_r10, friendships.`xfn_roomie` AS  
t1\_r11, friendships.`xfn_child` AS t1\_r12, friendships.`xfn_sibling` AS  
t1\_r13, friendships.`xfn_spouse` AS t1\_r14, friendships.`xfn_parent` AS  
t1\_r15, friendships.`xfn_muse` AS t1\_r16, friendships.`xfn_crush` AS  
t1\_r17, friendships.`xfn_date` AS t1\_r18, friendships.`xfn_sweetheart`  
AS t1\_r19, friends\_friendships.`id` AS t2\_r0,  
friends\_friendships.`login` AS t2\_r1, friends\_friendships.`email` AS  
t2\_r2, friends\_friendships.`crypted_password` AS t2\_r3,  
friends\_friendships.`salt` AS t2\_r4, friends\_friendships.`created_at` AS  
t2\_r5, friends\_friendships.`updated_at` AS t2\_r6,  
friends\_friendships.`remember_token` AS t2\_r7,  
friends\_friendships.`remember_token_expires_at` AS t2\_r8,  
friends\_friendships.`l_name` AS t2\_r9, friends\_friendships.`f_name` AS  
t2\_r10, friends\_friendships.`last_activity` AS t2\_r11,  
friends\_friendships.`last_activity_at` AS t2\_r12  
FROM users LEFT OUTER JOIN friendships ON friendships.user\_id =  
users.id LEFT OUTER JOIN users friends\_friendships ON  
friends\_friendships.id = friendships.friend\_id WHERE (users.`id` = 1) ):

########################################  
my method states:  
########################################

def index  
#retrieves all friendships and friends for the specified user\_id  
@user = User.find(params[:user\_id], :include =\> [:friendships =\>  
:friend])  
end

#######################################  
and the index view states:  
#######################################

\<% if logged\_in? and current\_user == @user %\> \<%= render :partial =\> 'friendship\_with\_edit', :collection =\> @user.friendships %\> \<% else %\> \<%= render :partial =\> 'friendship', :collection =\> @user.friendships %\> \<% end %\>

###################################### and finally the partial...friendship\_with\_edit ###################################### "\> \<%= link\_to friendship\_with\_edit.friend.login, user\_path(friendship\_with\_edit.friend), :class =\> 'xfnRelationship', :rel =\> xfn\_rel\_tag(@user, friendship\_with\_edit), :id =\> "friend-#{friendship\_with\_edit.friend.id}" %\> \<% if friendship\_with\_edit.friend.last\_activity\_at %\> \<%= friendship\_with\_edit.friend.last\_activity %\> \<%= time\_ago\_in\_words(friendship\_with\_edit.friend.last\_activity\_at) %\> ago \<% end %\> [\<%= link\_to 'edit friendship', edit\_friend\_url(:user\_id =\> @user, :id =\> friendship\_with\_edit.friend) %\>]

---

<div class="post-metadata">

**Author:** ![kaizerkaizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/45deac/32.png) [@kaizerkaizer](https://www.ruby-forum.com/u/kaizerkaizer)\
**Post date:** [February 29, 2008, 4:51am UTC](https://www.ruby-forum.com/t/unknown-column-friendships-id/130458/2 "2008-02-29T04:51:08Z")

</div>

Dag S. wrote:

> def index  
> #retrieves all friendships and friends for the specified user\_id  
> @user = User.find(params[:user\_id], :include =\> [:friendships =\>  
> :friend])

It appears that your :include is messed up.

Eager loading has it’s uses but it seems that lately it’s being used for  
everything that it shouldn’t be…

assuming you have the following relationship:  
class User \< ActiveRecord::Base  
has\_many :friendships  
has\_many :friends, :through =\> “friendships”  
end

class Friendship \< ActiveRecord::Base  
has\_many :users  
has\_many :friends, :class\_name =“User”  
end

just do:  
@user = User.find params[:user\_id]

and when you need the friends, just do  
@user.friends

hth

ilan
