Created
April 29, 2013 22:10
-
-
Save kineticac/5485204 to your computer and use it in GitHub Desktop.
Rails index_by to help order by the order of id arrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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