# Sort or Order a \<Select\> box

**URL:** <https://www.ruby-forum.com/t/sort-or-order-a-select-box/51470>\
**Category:** Rails\
**Created:** [December 31, 2005, 10:00pm UTC](https://www.ruby-forum.com/t/sort-or-order-a-select-box/51470 "2005-12-31T22:00:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![prot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a88e57/32.png) [@prot](https://www.ruby-forum.com/u/prot)\
**Post date:** [December 31, 2005, 10:00pm UTC](https://www.ruby-forum.com/t/sort-or-order-a-select-box/51470/1 "2005-12-31T22:00:08Z")

</div>

Evening folks,

I’m new to RoR, and have found some very useful info on this site,  
thanks.

I have a select box that I need to sort.

\<% @peoplekinds.each do |peoplekind| %\> \> \<%= peoplekind.name %\> \<% end %\>

In the controller for the List page I am able to use, :order\_by =\>  
‘name’ , under the Def List.

I have tried this in \<% @peoplekinds.each do |peoplekind| %\> above,  
but this does not work. How far off track am I?

Kindest regards.

---

<div class="post-metadata">

**Author:** ![prot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a88e57/32.png) [@prot](https://www.ruby-forum.com/u/prot)\
**Post date:** [December 31, 2005, 10:51pm UTC](https://www.ruby-forum.com/t/sort-or-order-a-select-box/51470/2 "2005-12-31T22:51:11Z")

</div>

> In the controller for the List page I am able to use, :order\_by =\>  
> ‘name’ , under the Def List.

When you retrieve @peoplekinds, either use sort\_by or pass the :order  
=\> ‘name’ parameter when you call your model’s find method.

You can also use options\_from\_collection\_for\_select(@peoplekinds,  
“id”, “name”, @people.peoplekind\_id) to generate all the   
tags for you; see [Peak Obsession](http://api.rubyonrails.com/classes/ActionView/)  
Helpers/FormOptionsHelper.html#M000356.

-Ben

---

<div class="post-metadata">

**Author:** ![prot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a88e57/32.png) [@prot](https://www.ruby-forum.com/u/prot)\
**Post date:** [January 1, 2006, 1:54am UTC](https://www.ruby-forum.com/t/sort-or-order-a-select-box/51470/3 "2006-01-01T01:54:30Z")

</div>

I’ve found this kind of construct works (where arg might be your AR).

options\_for\_select(arg.sort{|k,v| k[1] \<=\> v[1]}, selected)

So, perhaps you’d be using:

options\_for\_select(people.sort{|k,v| k[1] \<=\> v[1]},  
@people.peoplekind\_id)

Hope this helps…

---

<div class="post-metadata">

**Author:** ![prot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a88e57/32.png) [@prot](https://www.ruby-forum.com/u/prot)\
**Post date:** [January 2, 2006, 6:47am UTC](https://www.ruby-forum.com/t/sort-or-order-a-select-box/51470/4 "2006-01-02T06:47:50Z")

</div>

Thanks for the info, and thanks to those that emailed me with help.

The above two examples seem to be beyond me, but made for interesting  
reading, I have homework to do.

The soloution I came up with was to include the following under the  
controller for the page in question,

@peoplekind\_pages, @peoplekinds = paginate(:peoplekinds, :order\_by =\>  
‘name’, :per\_page =\> 30)

This seems to work but I’m gussing it is not a very ellegant approach,  
as I will have to repeat this under all the

def’s …

end

that it needs to appear in.

Thanks again.
