Skip to content

Instantly share code, notes, and snippets.

@kineticac
Created April 29, 2013 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kineticac/5485204 to your computer and use it in GitHub Desktop.
Save kineticac/5485204 to your computer and use it in GitHub Desktop.
Rails index_by to help order by the order of id arrays
ids = [111, 22, 332, 34, 445]
athletes = Athlete.where(:id => ids).index_by(&:id)
unless athletes.empty?
athletes = ids.map{|id| athletes[id]}
athletes.compact!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment