Ruby on Rails:如何对collection_select进行排序

Dan*_*Ruf 6 ruby sorting ruby-on-rails ruby-on-rails-3

我想通过数据库表列"播放"排序/排序它(desc或asc我想要它)我完全糊涂了.刚刚找到了select而不是collection_select的解决方案?

我的观点的一些代码

<%= f.collection_select :player1, Player.all, :id, :name %>
Run Code Online (Sandbox Code Playgroud)

不知道如何排序/订购

数据库表中还有一些列,如"play","goals"......

Hck*_*Hck 19

只需将实际排序的集合传递给collection_select帮助器:

collection_select(:post, :author_id, Author.order('created_at DESC'), :id, :name_with_initial, :prompt => true)
Run Code Online (Sandbox Code Playgroud)

因此,在您的源示例中,它将如下所示:

<%= f.collection_select :player1, Player.order('plays DESC'), :id, :name %>
Run Code Online (Sandbox Code Playgroud)