Convert an enumerable to a hash
In Ruby if you want to easily convert your enumerable object into a hash, use index_by.
This uses the block result as the key and the element as the value.
Here is an example:
# In this example, `posts` is an array of Post objects
posts.index_by(&:slug)
=> { "slug-1" => <Post ...>, "slug-2" => <Post ...>, ...}
Tweet