LoginSignup
0
0

More than 3 years have passed since last update.

postgresで任意のid順にorder by

Posted at

任意のid順にorder by

例えば手元にこのようなidsがあり、このidsの順番でuserを取得したいとき

ids = [2,3,1,10,9]

これでうまくいくかも

users = User.order("COALESCE(array_position(ARRAY[#{ids.join(",")}], id), 0) asc")

たまにこういうこと並び順が必要になること
ありますよね

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