ツイート機能やタイムライン機能をwebアプリケーションに実装した時、新しく投稿すると下に表示されてしまいます。
その順序を並び替えるのに「orderメソッド」を使用します。その使い方を説明します。
orderメソッド
テーブルから取得してきたインスタンス群を並び替えることができるメソッドです。
引数に「テーブルカラム名、並び替える順序」を記述して変化させる。
書き方例
def index
@tweets = Tweet.order("created_at DESC")
end
上の記述でツイートを作成順に逆から並べると言う意味になります。
並び替える順序
- ASC : 昇順
- DESC : 降順