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]
Another possibility for Postgres (9.4 or later):
ordered_ids = [1, 100, 5, 30, 4, 2, 88, 44]
User.joins("join unnest('{#{ordered_ids.join(',')}}'::int[]) WITH " \
"ORDINALITY t(id, ord) USING (id)").reorder('t.ord')
Notice that the reorder is extremely important.
Solution based on https://stackoverflow.com/a/35456954