Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort the options_for_select hash?

I’ve got something like this:

options_for_select({ "1 - optimal" => 1, "2 - ausreichend" => 2, "3 - verbesserungsfähig" => 3, "4 - nicht ausreichend" => 4, "5 - gar nicht" => 5})

Rails doesn’t sort these entries on output. How can I achieve the select field to be sorted numerically?

like image 868
Ulf Klose Avatar asked Feb 08 '11 23:02

Ulf Klose


1 Answers

I believe in Ruby1.9, this would work as you intend (hashes preserve their insertion order), so if using 1.9 is an option then you are done.

Otherwise, you can use an array instead of a hash:

options_for_select([["1 - optimal", 1], ["2 - ausreichend", 2], ..., ["5 - gar nicht", 5]])
like image 114
cam Avatar answered Oct 14 '22 18:10

cam