Rails iteration over arrays and hashes

ruby iterate over array of hashes
ruby iterate over hash
ruby array of hashes get value
ruby hash
ruby hash of hashes
ruby select value from array of hashes
ruby create hash in loop
ruby get value from hash by key

I am trying to iterate over what I think is a hash in rails. I used deep_pluck gem to get the values of a Model including some values from the associations. Like this:

    Continent.deep_pluck(:link_name, :name, 'countries' => [:link_name, :name])

this returns:

   [{"link_name"=>"europe", "name"=>"europe", "countries"=>[{"link_name"=>"countryname", "name"=>"countryname"}]}]

I am trying to iterate this like this:

             <% results.each do |continent| %>
              <% continent.each do |link_name, name, countries| %>
                <%= link_name %>
              <% end %>

            <% end %>

which in return this:

                link_name
                name
                countries

I tried a lot of different ways and methods and I still can't get the desired output which is to print out the values of :link_name and :name and also another object of countries to iterate them over again for their name etc.

I'm trying to build a navigation menu which has a list of each continent with a submenu of the countries of the continent. I use pluck because I just want the names and link_names of the database.

Thank you very much!

When you iterate over a hash, you get access to the keys and values.

If you look at what's printing out, you're getting the keys of each hash. Your output might make more sense if you set the parameters to what they actually are.

<% results.each do |continent| %>
  <% continent.each do |key, value| %>
    <%= key %>
  <% end %>
<% end %>

This should be enough to get you further along. To achieve what you want, you'll need to check each key and do something with the value based on what the key is.

Hash Iteration | Ruby Loops, Arrays aren't the only Ruby type that get to see the benefits of iteration. We can iterate over hash keys using the each_key method, which takes one argument: Ruby Language Iterating Over a Hash Example A Hash includes the Enumerable module, which provides several iteration methods, such as: Enumerable#each , Enumerable#each_pair , Enumerable#each_key , and Enumerable#each_value .

Perhaps try something like:

[{"link_name"=>"europe", "name"=>"europe", "countries"=>[{"link_name"=>"countryname", "name"=>"countryname"}]}].each do |continent|
  continent.each do |k,v|
    case k
    when 'countries'
      v.each do |country|
        country.each do |k,v| 
          puts "  - #{v}"
        end
      end
    else
      puts v
    end
  end
end

(Naturally, you'll need to add the erb markup. And the "puts" are just for example to run this in console.)

Which will give you:

europe
europe
  - countryname
  - countryname

Arrays and hashes in Ruby - DEV, You can loop through arrays and hashes by using the each method. The only difference between the two is that you can access both the key and the value when you are looping through a hash. How to iterate over a array in Ruby The each construct provides an easy way to iterate over arrays and hash. each loop will continue until it has gone through every item in the array. We have an associative array that stores the names of fruits.

Thank you very much for your suggestions, i finally managed to make it work like this:

      <% results.each do |continent| %>
             <li class="has_children"><a href="/continents/<%= continent["link_name"] %>"><%= continent["name"] %></a>
              <ul class="sub_menu">
                <% continent["countries"].each do |country| %>
            <li><a href="/countries/<%= country["link_name"] %>"><%= country["name"] %></a>
                <% end %>


              </ul>

             </li>

            <% end %>

Ruby Primer - Hashes, in and out., Iterating over a Hash. You can use the each method to iterate over all the elements in a Hash. However unlike Array#each , when you iterate over a Hash using  Here you go - trick is to remember that even though it's nested, it can still be iterated over inside another iteration it helps to use byebug, so when I got to the part of getting the year values to display, I just placed <% byebug %> right below <% item[:years].each do |year| %> so I could then see how the objects are being presented and adjust as necessary.

Ruby Hashes - A Detailed Guide, Understand Ruby Hashes and common Ruby Hash methods. We also cover how to iterate over a hash and how to compare Array vs Hash in Ruby. Hashes Hashes are another way to store multiple values inside a variable. In an array, you don't have any control over the indexes. They are numbers, and they go up by one with each item, starting from 0. In a hash, you provide key-value pairs, where the key doesn't have to be a number. To create a new hash, you have two possiblities:

Ruby Language, ruby documentation: Iterating Over a Hash. .each and .each_pair iterate over each key-value pair: h = { "first_name" => "John", "last_name" => "Doe" } h.each  Iterating over hashes is similar to iterating over arrays with some small differences. We'll use the each method again and this time we'll create a new file to test this out. We use the each method like before, but this time we assign a variable to both the key and the value.

Arrays and Hashes, Learn about arrays and hashes, including how to iterate over these data structures. In Ruby, a hash is a collection of key-value pairs. A hash is denoted by a  Iteration is defined as the act or process of repeating. It is where a computer is instructed to preform a certain process over and over again for a specific number of times or until a specific condition has been met/reached. For each iteration example in Arrays and Hashes below we will use the each method.

Comments
  • Thank you very much, i tried that and end up that I have to use continent["link_name"]