I am thinking about the best solution for a problem. Let\'s say that we have a list of ids of ActiveRecord model:
ids = [1, 100, 5, 30, 4, 2, 88, 44]
If you are using Postgres you can use intarray
intarray
class User < ActiveRecord::Base def self.find_in_order(ids) self.where(id: ids).order("idx(array[#{ids.join(',')}], id)") end end
you should init module first
CREATE EXTENSION intarray