ruby on rails - Order Users with most Songs Desc -
hopefully simple question. have several models, 2 of them, :users
, :songs
, interact data database.
a user has_many :songs
.
i'm trying find users songs in user index
action, i.e list 10 users songs, user songs @ top, descending.
so far have in users_controller
in index
;
@users = user.all
and far can in view, users/index
;
<% @users.each |user| %> <%= user.name %><%= user.songs.count %> <% end %>
works, counts songs how order users songs?
i have been looking @ sort_by inside block , guess done listing songs , grouping them user, feel not efficient enough. -as can see i'm not advanced developer.
please tell me guys think , answer solution if possible. thing confusing me ordering list generating table data generated table. must simple haven't done before can't head around it.
thanks in advance.
how
user.all(:limit => 10, :order => "(select count(user_id) songs user_id = users.id) desc")
Comments
Post a Comment