2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Rails]いいね数順でランキングかつページネーション

Posted at

以前このような記事を書きました。
[Rails]いいね数順でランキング

この内容に加え、ページネーションを実装したのでメモとして残しておきます。
今回はいいね数順で並び替えた投稿を1ページに5つ表示させるページネーションの実装を目指します。

前提

  • 投稿のテーブルは posts、 ユーザーのテーブルは users とする。
  • いいね機能に必要な中間テーブルは likes とする。
  • postsuserslikes それぞれのテーブルはいずれも作成済みとする。
  • ページネーションはKaminariというgemを使用して実装

手順

モデルにアソシエーションを定義

post.rb
class Post < ApplicationRecord
    belongs_to :user
    has_many :likes, dependent: :destroy
    has_many :liked_users, through: :likes, source: :user
end
user.rb
class User < ApplicationRecord
  has_many :posts,dependent: :destroy
  has_many :likes, dependent: :destroy
  has_many :liked_posts, through: :likes, source: :post
end
like.rb
class Like < ApplicationRecord
  belongs_to :post
  belongs_to :user
end

Kaminari をGemfileに追記し, bundle install

Gemfile
gem 'kaminari', '~> 0.17.0'
$ bundle install

コントローラーをいじる

app/controller/posts_controller.rb
def index
  posts = Post.includes(:liked_users).sort {|a,b| b.liked_users.size <=> a.liked_users.size}
  @posts = Kaminari.paginate_array(posts).page(params[:page]).per(5)
end

ここでは sort というrubyのメソッドを使って順序を操作している。
=> sortに関してはこちらを参照

a.liked_users.sizeb.liked_users.size が表しているのはそれぞれ各投稿のいいね数。
すなわち、各投稿のいいね数を比較して昇順で並び替えている。

sortメソッドによって生成されるpostsという変数は配列のデータなので、paginate_arrayというメソッドを使用している。

ビューで表示させる

あとはビューで表示させるだけ。

app/view/posts/index.html
<% @posts.each do |post| %>

  #省略

<% end %>
<%= paginate @posts %>

まだまだ知らないメソッドたちはたくさんあるなぁ。

参考

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?