This should be a simple problem, but I just can’t seem to find a good solution. I have a list of id’s that the user is selecting, and I want to preserve that order for the foos.
foo_ids = [1899, 7, 1, 2, 3, 42]
foos = Foo.find(foo_ids)
Any ideas on how to either preserve the order on select, or resort after the result set is returned?
I don’t think there is a way to do that with ActiveRecord.
However, here is what I would do:
I use Person.unscoped because in any case, your default scope is not the one you want. So it may be a little (very little) bit faster.
Using find(people_ids) produce only one query no matter how big is people_ids.
The worst would be to do something like this:
Because it produces a query for every id.
Sorry not to have the perfect answer 😉