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

时间:2022-07-19 15:59:14

I want to sort / order it (desc or asc how i want it) by the database table column "plays" Im totally confused. Just found a solution for select but not collection_select?

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

some code of my view

我的观点的一些代码

<%= f.collection_select :player1, Player.all, :id, :name %>

dont know how to sort / order

不知道如何排序/订购

there are also columns in the database table like "plays", "goals" ...

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

1 个解决方案

#1


19  

Just pass actually ordered collection to collection_select helper:

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

collection_select(:post, :author_id, Author.order('created_at DESC'), :id, :name_with_initial, :prompt => true)

So, in your source example it will look like this:

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

<%= f.collection_select :player1, Player.order('plays DESC'), :id, :name %>

#1


19  

Just pass actually ordered collection to collection_select helper:

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

collection_select(:post, :author_id, Author.order('created_at DESC'), :id, :name_with_initial, :prompt => true)

So, in your source example it will look like this:

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

<%= f.collection_select :player1, Player.order('plays DESC'), :id, :name %>