# Multiple select\_tag

**URL:** <https://www.ruby-forum.com/t/multiple-select-tag/57505>\
**Category:** Rails\
**Created:** [April 11, 2006, 4:38pm UTC](https://www.ruby-forum.com/t/multiple-select-tag/57505 "2006-04-11T16:38:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7bcc69/32.png) [@mich](https://www.ruby-forum.com/u/mich)\
**Post date:** [April 11, 2006, 4:38pm UTC](https://www.ruby-forum.com/t/multiple-select-tag/57505/1 "2006-04-11T16:38:42Z")

</div>

Hello again,

I have a table called line\_items, with the following fields:

id int  
item\_name varchar()  
qty int  
conditions text

I’ve made my LineItem model, and everything works as such (with simple  
text\_fields)…  
Now I’d like for the conditions field to be a select\_tag() with multiple  
choice possibility.

I’m guessing I need some code to expand/restract the array for the  
conditions - or maybe not ?

Any help much appreciated,

/mich

---

<div class="post-metadata">

**Author:** ![mich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7bcc69/32.png) [@mich](https://www.ruby-forum.com/u/mich)\
**Post date:** [April 11, 2006, 6:08pm UTC](https://www.ruby-forum.com/t/multiple-select-tag/57505/2 "2006-04-11T18:08:52Z")

</div>

This will get you a multi-select for an array of conditions:

select\_tag(  
‘conditions[]’,  
options\_for\_select( [[ ‘Cond1’, 1], [‘Cond2’, 2], [‘Cond2’, 3] ]  
),  
:multiple =\> true )

The conditions array will be available in params[:conditions]

options\_for\_select will take any container, so you could easily pull  
those values from the database if that’s where you have them.

Hope that helps…

Jeff

---

<div class="post-metadata">

**Author:** ![mich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7bcc69/32.png) [@mich](https://www.ruby-forum.com/u/mich)\
**Post date:** [April 12, 2006, 11:18am UTC](https://www.ruby-forum.com/t/multiple-select-tag/57505/3 "2006-04-12T11:18:55Z")

</div>

Jeff Everett wrote:

> This will get you a multi-select for an array of conditions:
> 
> select\_tag(  
> ‘conditions[]’,  
> options\_for\_select( [[ ‘Cond1’, 1], [‘Cond2’, 2], [‘Cond2’, 3] ]  
> ),  
> :multiple =\> true )
> 
> The conditions array will be available in params[:conditions]
> 
> options\_for\_select will take any container, so you could easily pull  
> those values from the database if that’s where you have them.

Hmm… What I’d to achieve is a way for a line\_item to have more than one  
condition. So let’s say that the line\_item: Car - can have a condition  
as NEW and USED.

I’m uncertain how to tell the model to wrap those two conditions into  
something I can stick into my database table.  
In addition, the model should also be able to “expand” the information  
from the table, before parsing it to the view/controller.

Any ideas ?

/mich

---

<div class="post-metadata">

**Author:** ![mich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7bcc69/32.png) [@mich](https://www.ruby-forum.com/u/mich)\
**Post date:** [April 12, 2006, 6:32pm UTC](https://www.ruby-forum.com/t/multiple-select-tag/57505/4 "2006-04-12T18:32:01Z")

</div>

It sounds like you want a Has-And-Belongs-To-Many (habtm) relationship  
between line\_items and conditions. You can check out:

[http://jrhicks.net/96](http://jrhicks.net/96)

for a pretty good tutorial on how to set those up in the database and  
getting that info into a view. Or search this archive or google for  
habtm

Jeff

---

<div class="post-metadata">

**Author:** ![mich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7bcc69/32.png) [@mich](https://www.ruby-forum.com/u/mich)\
**Post date:** [April 13, 2006, 4:02pm UTC](https://www.ruby-forum.com/t/multiple-select-tag/57505/5 "2006-04-13T16:02:12Z")

</div>

I still think you’d probably be better off using a habtm relationship  
for this, it would certainly be the most Rails-ish way to go about it.  
( It always worries when someone tells me a business rule is “fixed”  
8) )

But having said that, if you’re using MySQL what you’re describing is  
a lot like the SET column type, which is not standard SQL. There are  
some tips and options for using this with Rails here:

[http://wiki.rubyonrails.com/rails/pages/HowtoUseSetAndEnumColumns](http://wiki.rubyonrails.com/rails/pages/HowtoUseSetAndEnumColumns)

My gut feeling is if you go down this route, it’s going to be very  
difficult to not duplicate code in different parts of the app, and you  
will probably end up having to override rather low level Rails methods  
( like save and update ), or write various custom methods to replace  
them. But that’s just my 2¢…

Jeff

---

<div class="post-metadata">

**Author:** ![mich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7bcc69/32.png) [@mich](https://www.ruby-forum.com/u/mich)\
**Post date:** [April 13, 2006, 1:10pm UTC](https://www.ruby-forum.com/t/multiple-select-tag/57505/6 "2006-04-13T13:10:32Z")

</div>

Jeff Everett wrote:

> It sounds like you want a Has-And-Belongs-To-Many (habtm) relationship  
> between line\_items and conditions. You can check out:

Well, not exactly. The condtions “model” is fixed and will not change,  
so I was not considering sticking it into a table.

I’ve used your previous example above, and I have the conditions[] array  
available in the controller, where I can do a:

params[:conditions].each { |con| @line\_item.condition \<\< con }

Which works. I could probably delimit it with fx. ‘::’ - so my table  
field would look like so: NEW::USED::RETAIL - and when grabbing it from  
db write a similar mechanism to extract each condition. However, I am  
not too pleased about having this code in the controller, it would be  
more appropiate having it in the model (line\_items)

So how do work with the params[:conditions] array in the model ?

By the way, many thanks for your help so far !

/mich
