# Collection\_select with HABTM relationship

**URL:** <https://www.ruby-forum.com/t/collection-select-with-habtm-relationship/143603>\
**Category:** Rails\
**Created:** [July 21, 2008, 4:47am UTC](https://www.ruby-forum.com/t/collection-select-with-habtm-relationship/143603 "2008-07-21T04:47:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![adamdorsey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@adamdorsey](https://www.ruby-forum.com/u/adamdorsey)\
**Post date:** [July 21, 2008, 4:47am UTC](https://www.ruby-forum.com/t/collection-select-with-habtm-relationship/143603/1 "2008-07-21T04:47:28Z")

</div>

I have a collection set…

\<%= collection\_select(:bet, :opponent\_id, Friendship.find(:all,  
:conditions =\> [“user1\_id = ?”, current\_user], :order =\> “id”) , :id,  
:id) %\>

…wors fine. But I want to return the users first+last name based on the  
user -\> frienship relationship.

I tried to do something like this…

\<%= collection\_select(:bet, :opponent\_id, Friendship.find(:all,  
:conditions =\> [“user1\_id = ?”, current\_user], :order =\> “id”) , :id,  
@bet.user.first\_name) %\>

It actually returns the correct First name, but throws an error, because  
it thinks the actual name is an undefined method.

??

---

<div class="post-metadata">

**Author:** ![adamdorsey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ed8c4c/32.png) [@adamdorsey](https://www.ruby-forum.com/u/adamdorsey)\
**Post date:** [July 21, 2008, 5:16pm UTC](https://www.ruby-forum.com/t/collection-select-with-habtm-relationship/143603/2 "2008-07-21T17:16:37Z")

</div>

Let’s see the relevant bits of your User and Friendship models. If you  
define the relationship correctly, AR should give you e.g., a .friends  
method on your User instances, which should return a collection of User  
objects. You should be able to do something like:

collection\_select(:bet, :opponent\_id, current\_user.friends, :id,  
:full\_name)

-Roy
