4
7

More than 5 years have passed since last update.

postの表示の順番

Last updated at Posted at 2016-02-20

これをつけることによって
投稿順が新しい物が上に来るようになる

post.rb

 default_scope -> { order(created_at: :desc) }

別のやり方もある。

Post.all.order(created_at: :dsc)降順

Post.all.order(created_at: :asc)昇順

orderメソッドは、取得したデータを並び替えるメソッドで、
orderの引数にはハッシュを渡し、「order(並び替える基準とするカラム名: :並べ方)」のように指定する。並べ方には「desc(降順)」と「asc(昇順)」がある

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