LoginSignup
0
0

More than 1 year has passed since last update.

Railsで特定のカラムについて、モデル内での順位を取得する方法

Last updated at Posted at 2019-02-14

Railsで特定のカラムについて、モデル内での順位を取得する方法

例えば、Channelモデルについてsubscriver_countカラムで見たときの順位を取得する方法を解説します。

@channel = Channel.find(params[:id])
@ranking = Channel.where('subscriber_count >= ?', @channel.subscriber_count).count

このように、whereメソッドとcountメソッドを使うことで達成できます。

終わりに

私は現在、Web3のサービスの開発をしています。詳しくはこちらの記事をご覧下さい。
無料でイーサリアムが当たる、Web3時代の寄付サイトを作った話

0
0
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
0
0