# Better way to concatenate options to collection\_select?

**URL:** <https://www.ruby-forum.com/t/better-way-to-concatenate-options-to-collection-select/184741>\
**Category:** Rails\
**Created:** [March 7, 2010, 7:41pm UTC](https://www.ruby-forum.com/t/better-way-to-concatenate-options-to-collection-select/184741 "2010-03-07T19:41:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![oshanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/2acd7d/32.png) [@oshanter](https://www.ruby-forum.com/u/oshanter)\
**Post date:** [March 7, 2010, 7:41pm UTC](https://www.ruby-forum.com/t/better-way-to-concatenate-options-to-collection-select/184741/1 "2010-03-07T19:41:34Z")

</div>

I’ve just been through 4 pages of similar threads, but nothing quite  
satisfying.

What I’m trying to achieve:

 Please select ALL Forum 1 Forum 2 ...

The way I’m currently doing it:

# Plugin

class FakeModel  
def initialize(hash)  
@hash = hash  
end

def id  
@hash[:id]  
end

def method\_missing(meth\_id)  
@hash[meth\_id]  
end  
end

# Controller

@forums = [  
FakeModel.new(:id =\> -1, :name =\> ‘Please select’),  
FakeModel.new(:id =\> 0, :name =\> ‘ALL’)  
] + Forum.all(:order =\> ‘name’)

# View

\<%= collection\_select :thread, ‘forum\_id’, @forums, :id, :name %\>

Just wondering if there’s a more natural (and undocumented) way of doing  
it…

---

<div class="post-metadata">

**Author:** ![oshanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/2acd7d/32.png) [@oshanter](https://www.ruby-forum.com/u/oshanter)\
**Post date:** [March 7, 2010, 8:51pm UTC](https://www.ruby-forum.com/t/better-way-to-concatenate-options-to-collection-select/184741/2 "2010-03-07T20:51:02Z")

</div>

On Mar 7, 6:41 pm, Jack o’Shanter [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> # View
> 
> \<%= collection\_select :thread, ‘forum\_id’, @forums, :id, :name %\>
> 
> Just wondering if there’s a more natural (and undocumented) way of doing  
> it…

In a small experiment,

collection\_select :thread, ‘forum\_id’, @forums, :id, :name, :prompt =\>  
true, :include\_blank =\> ‘All’

seems to work for me. If that doesn’t work out, i’d use select instead  
of collection\_select - collection\_select is trying to make one use  
case of select easier, but if you’re jumping through hoops then it’s  
no longer easier than just using select.

Fred

---

<div class="post-metadata">

**Author:** ![oshanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/2acd7d/32.png) [@oshanter](https://www.ruby-forum.com/u/oshanter)\
**Post date:** [March 8, 2010, 6:35pm UTC](https://www.ruby-forum.com/t/better-way-to-concatenate-options-to-collection-select/184741/3 "2010-03-08T18:35:44Z")

</div>

Frederick C. wrote:

> In a small experiment,
> 
> collection\_select :thread, ‘forum\_id’, @forums, :id, :name, :prompt =\>  
> true, :include\_blank =\> ‘All’
> 
> seems to work for me. If that doesn’t work out, i’d use select instead  
> of collection\_select - collection\_select is trying to make one use  
> case of select easier, but if you’re jumping through hoops then it’s  
> no longer easier than just using select.

Overwriting ActionView::Helpers::InstanceTag#add\_options() could also  
work:

module ActionView

# Find original in

/usr/lib64/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action\_view/helpers/form\_options\_helper.rb  
module Helpers  
class InstanceTag  
private  
def add\_options(option\_tags, options, value = nil)  
if options[:start\_with]  
options[:start\_with].reverse\_each do |element|  
option\_text, option\_value = option\_text\_and\_value(element)  
selected\_attribute = ’ selected=“selected”’ if  
options[:selected] && option\_value\_selected?(option\_value,  
options[:selected])  
option\_tags = “\<option  
value=”#{html\_escape(option\_value.to\_s)}"#{selected\_attribute}\>#{html\_escape(option\_text.to\_s)}\n"

- option\_tags  
end  
end  
if options[:end\_with]  
options[:end\_with].reverse\_each do |element|  
option\_text, option\_value = option\_text\_and\_value(element)  
selected\_attribute = ’ selected=“selected”’ if  
options[:selected] && option\_value\_selected?(option\_value,  
options[:selected])  
option\_tags += “\<option  
value=”#{html\_escape(option\_value.to\_s)}"#{selected\_attribute}\>#{html\_escape(option\_text.to\_s)}\n"  
end  
end  
if options[:include\_blank]  
option\_tags = “\<option value=”"\>#{options[:include\_blank]  
if options[:include\_blank].kind\_of?(String)}\n" + option\_tags  
end  
if value.blank? && options[:prompt]  
prompt = options[:prompt].kind\_of?(String) ?  
options[:prompt] : I18n.translate(‘support.select.prompt’, :default =\>  
‘Please select’)  
“\<option value=”"\>#{prompt}\n" + option\_tags  
else  
option\_tags  
end  
end  
end  
end  
end

Now I could do:

:start\_with =\> [[’- Select category -’, -1], [’- All -’, 0]]

However, select() might be a wiser approach.
