Oddly, no one has suggested something like this:
index = Something.find(array_of_ids).group_by(&:id)
array_of_ids.map { |i| index[i].first }
As efficient as it gets besides letting SQL backend do it.
Edit: To improve on my own answer, you can also do it like this:
Something.find(array_of_ids).index_by(&:id).slice(*array_of_ids).values
#index_by
and #slice
are pretty handy additions in ActiveSupport for arrays and hashes respectively.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…