Created
November 3, 2013 16:29
-
-
Save warolv/7292030 to your computer and use it in GitHub Desktop.
Rails’ index_by: the easy way to convert an Array to a Hash
This file contains 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
Post.all.index_by { |post| post.id } | |
# => { 1 => #<Post ...>, 2 => #<Post ...>, ... } | |
Post.all.index_by(&:title) | |
# => { "My first post" => #<Post ...>, "My second post" => #<Post ...>, ... } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment