# Select dropdown based off join table's include?

**URL:** <https://www.ruby-forum.com/t/select-dropdown-based-off-join-tables-include/139167>\
**Category:** Rails\
**Created:** [May 31, 2008, 8:07pm UTC](https://www.ruby-forum.com/t/select-dropdown-based-off-join-tables-include/139167 "2008-05-31T20:07:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/65b543/32.png) [@Sav](https://www.ruby-forum.com/u/Sav)\
**Post date:** [May 31, 2008, 8:07pm UTC](https://www.ruby-forum.com/t/select-dropdown-based-off-join-tables-include/139167/1 "2008-05-31T20:07:49Z")

</div>

I have a “New Photo” view where I’d like to provide a select dropdown  
of the user’s favorite locations. Getting the data for a user’s  
favorites is no problem, but I’m not sure how to put the location name  
into the select form.

def new  
@photo = Photo.new  
@favorites = current\_user.favorites.find(:all, :include  
=\> :location)  
end

\<%= f.collection\_select :location\_id,  
@favorites, :location\_id, :location\_id, { :include\_blank =\> true }, {}  
%\>

(the 4th parameter in the f.collection\_select above is my problem)

On a related note, is there a more efficient way to query for the  
locations’ id & name w/o returning all fields? Or is it not really  
that much of a performance issue?

Any tips would be greatly appreciated. Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Sav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/65b543/32.png) [@Sav](https://www.ruby-forum.com/u/Sav)\
**Post date:** [May 31, 2008, 11:20pm UTC](https://www.ruby-forum.com/t/select-dropdown-based-off-join-tables-include/139167/2 "2008-05-31T23:20:25Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

On Saturday 31 May 2008 20:06:58 Sav wrote:

> Getting the data for a user’s  
> favorites is no problem, but I’m not sure how to put the location  
> name into the select form.

Add location\_name as a method on User:

def location\_name  
self.location.name  
end

Then you can pass :location\_name to collection\_select.

Ciao,  
Sheldon.  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFIQcDWpGJX8XSgas0RAgwCAJ9bRiEJ/EJau/YZ/NSXwHeF6aEHyACgqGRa  
5L//c/Q5jNVZzN4DsYRrQ2c=  
=UNXf  
-----END PGP SIGNATURE-----
