# Collection\_select

**URL:** <https://www.ruby-forum.com/t/collection-select/137585>\
**Category:** Rails\
**Created:** [May 14, 2008, 12:26pm UTC](https://www.ruby-forum.com/t/collection-select/137585 "2008-05-14T12:26:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![martinevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@martinevans](https://www.ruby-forum.com/u/martinevans)\
**Post date:** [May 14, 2008, 12:26pm UTC](https://www.ruby-forum.com/t/collection-select/137585/1 "2008-05-14T12:26:11Z")

</div>

Hi is it possible to manually add an option to collection\_select  
I have the following code and want to add “Add Customer” to the bottom  
of the options list.

\<% @customers = Customer.find(:all, :order =\> “company”)%\>  
\<%= f.collection\_select(:customer\_id, @customers,:id, :company) %\>

Thanks  
Martin

---

<div class="post-metadata">

**Author:** ![martinevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@martinevans](https://www.ruby-forum.com/u/martinevans)\
**Post date:** [May 22, 2008, 6:06pm UTC](https://www.ruby-forum.com/t/collection-select/137585/2 "2008-05-22T18:06:26Z")

</div>

On 5/14/08, Martin E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi is it possible to manually add an option to collection\_select  
> I have the following code and want to add “Add Customer” to the bottom  
> of the options list.
> 
> \<% @customers = Customer.find(:all, :order =\> “company”)%\>

@customers is an array, so you want to use the array method ‘+’ to add  
your new element to it.

[http://www.ruby-doc.org/core/classes/Array.html#M002232](http://www.ruby-doc.org/core/classes/Array.html#M002232)

> \<%= f.collection\_select(:customer\_id, @customers,:id, :company) %\>

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![martinevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@martinevans](https://www.ruby-forum.com/u/martinevans)\
**Post date:** [May 26, 2008, 10:45pm UTC](https://www.ruby-forum.com/t/collection-select/137585/3 "2008-05-26T22:45:03Z")

</div>

Greg D. wrote:

> On 5/14/08, Martin E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Hi is it possible to manually add an option to collection\_select  
> > I have the following code and want to add “Add Customer” to the bottom  
> > of the options list.
> > 
> > \<% @customers = Customer.find(:all, :order =\> “company”)%\>
> 
> @customers is an array, so you want to use the array method ‘+’ to add  
> your new element to it.
> 
> [class Array - RDoc Documentation](http://www.ruby-doc.org/core/classes/Array.html#M002232)
> 
> > \<%= f.collection\_select(:customer\_id, @customers,:id, :company) %\>
> 
> –  
> Greg D.  
> [http://destiney.com/](http://destiney.com/)  
> I can see that it is an array and I’ve looked at the documentation and  
> have tried a few ways of getting it to work but have failed miserably  
> can you give me an example of how I would add an option manually this is  
> what I have for the select  
> @customers = Customer.find(:all, :select =\> “id, company”)  
> =\> [#\<Customer id: 6, company: “Ernie’s Bakery”\>, #\<Customer id: 7,  
> company: “Young’s the Bakers”\>, #\<Customer id: 8, company: “Suggs  
> Newsagents”\>]

thanks  
Martin

---

<div class="post-metadata">

**Author:** ![martinevans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@martinevans](https://www.ruby-forum.com/u/martinevans)\
**Post date:** [May 28, 2008, 12:20am UTC](https://www.ruby-forum.com/t/collection-select/137585/4 "2008-05-28T00:20:43Z")

</div>

You probably want to get a little lower-level than collection\_select,  
which is designed to simplify creating your select tag from a  
collection of objects. Since you’re adding an option to that  
collection, you should go with either select\_tag, or select, adding  
options\_for\_select( Customer.find(:all).map{|c|[c.id,c.company} +  
[‘add\_a\_customer’, ‘Add Customer’])  
as the “options” parameter.

Take a look at the API for the usage of “select” or “select\_tag”. Also  
depends on whether you’re using a form helper.

On May 26, 3:45 pm, Martin E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
