25
21

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

Railsで複数項目での order by を簡単に記述する

Last updated at Posted at 2018-12-16

複数項目を条件にしたソートをActiveRecordで記述する場合に毎回検索して調べていましたが非常に簡単な方法がありました。
order を項目の数だけ記述すればいいのです。

User.order(:group).order(:name)

降順でソートする場合は以下のように書きます。

User.order(:group).order(birth: :desc)

Ruby on Rails Guides に記載されているのに何故か広まっていませんね。

Active Record クエリインターフェイス - 3 並び順

25
21
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
25
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?