Home>
The name can be displayed with the following description. How do you display the person's name in descending order of player_level? The image is like a ranking.

Controller

@users = User.pluck (: name)
<% @ users.each do | user |%>
           <% = user%>
          <% end%>
  • Answer # 1

    Sort by column value and retrieve by array of: name.

    User.order (player_level:: desc) .pluck (: name)

  • Answer # 2

    There are various methods, but for example
    If you only take name

    User.all.order ('count (player_level) desc'). limit (3) .pluck (: name)

    I think you can get it at.
    If you search by activerecord order or activerecord ranking, there are many articles that will be helpful, so please take a look.

Related articles