I've got a method that sorts a collection by an array of ids like so:
def selected_components
ids = @document.publications.rank(:position).map(&:component_id)
Component.find(ids).sort_by { |c| ids.index(c.id) }
end
This works fine, but I want to sort the result by the order of ids as efficiently as possible. Apparently my method is not the most efficient, although I'm not 100% why that is.
Why isn't this so efficient? Any advice? Thanks so much.