如何对f.select进行排序?

时间:2011-08-18 09:35:38

标签: ruby ruby-on-rails-3

<%= f.select(:company_id, Company.all.collect {|company| [company.name, company.id]}) %>

我如何从A-Z中对此进行排序?

1 个答案:

答案 0 :(得分:4)

<%= f.select :company_id, Company.all.collect{|company| [company.name, company.id]}.sort_by{|a, b| a} %>

或者

<%= f.select :company_id, Company.all.collect{|company| [company.name, company.id]}.sort{|a, b| a[0] <=> b[0]} %>

<强> UPD

或者只是<%= f.select :company_id, Company.all.map{ |c| [c.name, c.id] }.sort %> - Phrogz