Is it possible to get the Unique values of a Hash? Lets say I have a hash which one of the keys is “name” Name can come up a couple dozen times, but it will only have lets say 10 variations of itself all unique. What I want to do is be able to take those 10 unique names and build a view based off of them. But in order to roll the view out properly I would need to know those unique names. Im thinking I have to build an array, or another hash or something of the unique names. So I can go over that array with “each” then for each unique if the original hash has a match apply it to that section or block on the view. Hopefully this makes sense to someone. Who can help me out.
val = {
:status => "successful",
:service_list => [
{
:service_name => "oozie",
:status => "RUNNING",
:status_message => "Running Master Service",
:host => "1"
},
{
:service_name => "single-namenode",
:status => "RUNNING",
:status_message => "Running Service",
:host => "1"
},
{
:service_name => "single-database",
:status => "RUNNING",
:status_message => "Running Service",
:host => "1"
}
]}
truncated version of the hash I am working with.
Not sure if it is the right answer, but the list of unique service names can be obtained like this:
Update:
Iterating over the hash of services grouped by service_name is a little easier, here’s an example:
In the action of your controller:
In your view
Hope that helps