# FormOption Helper for Group Select

**URL:** <https://www.ruby-forum.com/t/formoption-helper-for-group-select/140443>\
**Category:** Rails\
**Created:** [June 14, 2008, 1:08am UTC](https://www.ruby-forum.com/t/formoption-helper-for-group-select/140443 "2008-06-14T01:08:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stevel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4491bb/32.png) [@stevel](https://www.ruby-forum.com/u/stevel)\
**Post date:** [June 14, 2008, 1:08am UTC](https://www.ruby-forum.com/t/formoption-helper-for-group-select/140443/1 "2008-06-14T01:08:27Z")

</div>

I am trying to generate the Form Select with the OPTGROUP tags, from a  
simple array. For example I have an array : [Acer, laptop, 1], [Acer,  
monitor, 2], [HP, desktop, 3], [HP, printer,4]

I know how to use the Select helper to generate the Select - Option  
tags from an array. But I just couldn’t figure out how to do with the  
OPTGROUP tag.

I would like to produce the final result from the array as:

 laptop monitor ... Desktop Printer ... 

I know there is a helper called  
option\_groups\_from\_collection\_for\_select. But from what I understand  
this helper is only based on the object relationships of its  
arguments. I would like to generate from a simple array, using a  
helper.

I have spend hours trying to figure this out but without success. Any  
help will be greatly appreciated.

Thanks in advance.

Steve

---

<div class="post-metadata">

**Author:** ![stevel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4491bb/32.png) [@stevel](https://www.ruby-forum.com/u/stevel)\
**Post date:** [June 14, 2008, 2:25pm UTC](https://www.ruby-forum.com/t/formoption-helper-for-group-select/140443/2 "2008-06-14T14:25:35Z")

</div>

On 14 Jun 2008, at 00:07, stevel wrote:

> I am trying to generate the Form Select with the OPTGROUP tags, from a  
> simple array. For example I have an array : [Acer, laptop, 1], [Acer,  
> monitor, 2], [HP, desktop, 3], [HP, printer,4]
> 
> I know how to use the Select helper to generate the Select - Option  
> tags from an array. But I just couldn’t figure out how to do with the  
> OPTGROUP tag.

You’ve just got completely the wrong data structure. Either coerce  
your data into the form expected by  
option\_groups\_from\_collection\_for\_select (eg use some structs; you may  
find group\_by helpful) or just do it all by hand

Fred

---

<div class="post-metadata">

**Author:** ![stevel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4491bb/32.png) [@stevel](https://www.ruby-forum.com/u/stevel)\
**Post date:** [June 20, 2008, 1:06am UTC](https://www.ruby-forum.com/t/formoption-helper-for-group-select/140443/3 "2008-06-20T01:06:54Z")

</div>

Fred, thanks for your input. Having spending sometime understanding  
option\_groups\_from\_collection\_for\_select, I agreed with you that my  
data structure above was all wrong.

Let me try again to describe my problem after approaching the problem  
differently.

I am trying to have a Select dropdown by groups whereby the content of  
this dropdown would be the names that come from a data table called  
User with the following attribs.

class User \< ActiveRecord::Base  
# attribs: id, name, location  
end

I have tried using the option\_groups\_from\_collection\_for\_select as  
follows:

option\_groups\_from\_collection\_for\_select(User.find(:all),  
User.find(:all, :order =\> ‘location DESC,  
name’), :location, :name, :name)

hoping to get something like the following result:

 Henry James ... Gordon Sally Thomas ... 

This doesn’t seem to be correct. If someone could help point out what  
I am not doing right, that would be most appreciated.

Thanks in advance.

On Jun 14, 5:24 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
