1
0

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 1 year has passed since last update.

【Rails】joinした先のテーブルで昇順降順のソートもしたい

Last updated at Posted at 2022-08-09

bad

joinした先のテーブルでソートする場合

User.eager_load(:posts)
    .order('posts.title')

という例をよくみるが、これだと文字列を使う&昇順・降順のソートができない。

better

mergeを使うことでjoinした先のテーブルで昇順・降順のソートができる。

User.eager_load(:posts)
    .merge(Post.order(title: :asc))

参考

1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?