Ranking display
ruby 2.5.7 Rails 5.2.4.3 OS: macOS Catalina
--Only controller and view changed --user model --post model (: body,: score) --Like function implementation --Comment sentiment analysis by Google Natural Language API has been introduced (post scoring)
controller If you can do it with a controller, it's almost possible!
Mean ranking: app/controllers/users_controller.rb
def rank
@users = User.
find(Post.
group(:score).
order('avg(score) desc').
pluck(:user_id)
)
end
Total ranking: app/controllers/posts_controller.rb
def rank
@posts = Post.
find(Favorite.
group(:post_id).
order('count(post_id) desc').
pluck(:post_id)
)
end
view Can be displayed in descending order of each as shown below
python
<% @users.each do |user| %>
<% end %>
<% @posts.each do |user| %>
<% end %>
If you add Kaminari.paginate_array () in front and .page (params [: page]) in the end, it's OK.
python
@posts = Kaminari.paginate_array(Post.find(Favorite.group(:post_id).order('count(post_id) desc').pluck(:post_id))).page(params[:page])
[Easy ranking function with Rails] (https://qiita.com/mitsumitsu1128/items/18fa5e49a27e727f00b4) [Rails] Implementation of ranking function
Recommended Posts