0
1

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 3 years have passed since last update.

orderメソッドでツイートを新しい順に ❏Rails❏

Last updated at Posted at 2019-11-25

orderを使うと、データベースから取得した複数のレコードを並び替えることができます。
今回はツイートの投稿日が新しいものが上に来るようにします。

#使い方

.order("カラム名 順序")

順序は、ASC(昇順)とDESC(降順)があります。

#使用例

tweets_controller
def index
  @tweet = Tweet.all.order("created_at DESC")
end

allですべて取得し、それを投稿日の降順で並び替えます。


また、allは省略できます。
tweets_controller
def index
  @tweet = Tweet.order("created_at DESC")
end


ではまた!
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?