0
0

More than 3 years have passed since last update.

Railsでのorderメソッドの説明

Last updated at Posted at 2020-07-11

ツイート機能やタイムライン機能をwebアプリケーションに実装した時、新しく投稿すると下に表示されてしまいます。

その順序を並び替えるのに「orderメソッド」を使用します。その使い方を説明します。

orderメソッド


テーブルから取得してきたインスタンス群を並び替えることができるメソッドです。

引数に「テーブルカラム名、並び替える順序」を記述して変化させる。


書き方例


def index
  @tweets = Tweet.order("created_at DESC")
end

上の記述でツイートを作成順に逆から並べると言う意味になります。

並び替える順序

  • ASC : 昇順
  • DESC : 降順

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