# Select options for HABTM?

**URL:** <https://www.ruby-forum.com/t/select-options-for-habtm/65159>\
**Category:** Rails\
**Created:** [February 10, 2006, 12:08am UTC](https://www.ruby-forum.com/t/select-options-for-habtm/65159 "2006-02-10T00:08:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![csn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@csn](https://www.ruby-forum.com/u/csn)\
**Post date:** [February 10, 2006, 12:08am UTC](https://www.ruby-forum.com/t/select-options-for-habtm/65159/1 "2006-02-10T00:08:04Z")

</div>

I’ve looked through the docs and the wiki and can’t  
figure out how to go about generating select options  
and the update function for an item that has a HABTM  
relationship.

I have a ‘parks’ table and the park model has a HABTM  
to the ‘states’ table. On the park edit page, I’d like  
a multiple select box to appear with all states show,  
and those in the parks\_states table selected. Does  
anybody have some example code?

Thanks!  
csn

* * *

Do You Yahoo!?  
Tired of spam? Yahoo! Mail has the best spam protection around

> **[Yahoo Mail - Organized Email](https://mail.yahoo.com/)**
>
> It's time to get stuff done with Yahoo Mail. Just add your Gmail, Outlook, AOL or Yahoo Mail to get going. We automatically organize all the things life throws at you, like receipts and attachments, so you can find what you need fast. Plus, we've got...

---

<div class="post-metadata">

**Author:** ![csn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@csn](https://www.ruby-forum.com/u/csn)\
**Post date:** [February 10, 2006, 2:12am UTC](https://www.ruby-forum.com/t/select-options-for-habtm/65159/2 "2006-02-10T02:12:33Z")

</div>

CSN \<cool\_screen\_name90001@…\> writes:

> 

I’d don’t use the multiple-choice select very often, but something like:

options\_from\_collection\_for\_select(State.find(:all), :id, :name,  
@park.states.collect {|s| s.id})

should work, right? Assuming State is your model, and the value of the  
option is  
State#id, and the text is State#name, and @park is your current Park.

---

<div class="post-metadata">

**Author:** ![csn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@csn](https://www.ruby-forum.com/u/csn)\
**Post date:** [February 10, 2006, 2:24am UTC](https://www.ruby-forum.com/t/select-options-for-habtm/65159/3 "2006-02-10T02:24:10Z")

</div>

— KW [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > I have a ‘parks’ table and the park model has a  
> > but something like:
> 
> options\_from\_collection\_for\_select(State.find(:all),  
> :id, :name,  
> @park.states.collect {|s| s.id})
> 
> should work, right? Assuming State is your model,  
> and the value of the option is  
> State#id, and the text is State#name, and @park is  
> your current Park.

Yeah - here’s what I came up with:

# edit template
 \<%= options\_from\_collection\_for\_select State.find(:all, :order=\>'name'), 'id', 'name', @park.states.collect {|state| state.id } %\>
# update method

@park = Park.find(params[:id])

@park.state\_ids = params[:park\_states]

@park.update\_attributes(params[:park])

if @park.errors.empty?  
redirect\_to :action=\>:list  
else  
render :action=\>:edit  
end

If anybody can improve and/or simplify it, please feel  
free! Perhaps I’ll stick it in the wiki.

csn

* * *

Do You Yahoo!?  
Tired of spam? Yahoo! Mail has the best spam protection around

> **[\[object](https://mail.yahoo.com/)**
>
> \[object
