Ruby on Rails: how to sort a_select collection

I want to sort / order it (desc or asc, as I want it) by the column of the database table "plays" I'm completely confused. Just found a solution for select, but not collection_select?

some code of my view

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

I don’t know how to sort / order

the database table also has columns such as "games", "goals" ...

+6
source share
1 answer

Just pass the ordered collection to collection_select:

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

So, in your original example, it would look like this:

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

Source: https://habr.com/ru/post/892133/


All Articles