1

I have an instance variable @report_antennas_control_access with this data [{:id_control_access=>1, :input=>"Antena 1"}, {:id_control_access=>1, :output=>"Antena 2"}, {:id_control_access=>1, :input=>"Antena 5"}, {:id_control_access=>2, :input=>"Antena 3"}, {:id_control_access=>2, :output=>"Antena 4"}] and I want to print it in my .html table but in doing so I do it in a way that I do not want

I do it this way:

                  <tbody>
                    <% @report_antennas_control_access.each do | antennas | %>
                      <tr>
                        <% if control_access[:id_control_access] == antennas[:id_control_access] %>
                          <td><%= antennas[:input] %></td>
                          <td><%= antennas[:output] %></td>
                        <% end %>
                      </tr>
                    <% end %>
                  </tbody>

But he prints it to me in a way I do not want:

enter image description here

This is the way I need to print that data (example):

enter image description here

2 Answers 2

3

As David already said, with your input, it will be really difficult to achieve what you need. Since it was fun, I fixed it, but I do believe that it should be fixed somewhere higher in your code (I hope you're using Ruby 2.5+, if not, let me know which version you are on).

def fix_my_data(data)
  data.group_by { |x| x[:id_control_access] }
      .transform_values do |v|
        v.map { |h| h.slice(:input, :output) }
         .group_by { |h| h.keys.first }.values.inject(:zip).map { |x,y| x.merge(y.to_h) }
      end
end

If you pass your array into this function, it will return this:

{1=>[{:input=>"Antena 1", :output=>"Antena 2"}, {:input=>"Antena 5"}],
 2=>[{:input=>"Antena 3", :output=>"Antena 4"}]}

Which should be really simple to generate HTML with, like so:

<tr>
  <% @data[control_access[:id_control_access]].each do |antenna| %>
    <td><%= antenna[:input] %></td>
    <td><%= antenna[:output] %></td>
  <% end %>
</tr>

I'm pretty sure fix_my_data can be written in a bit simpler way, but as I mentioned, it's a late place to be fixing the data.

Sign up to request clarification or add additional context in comments.

1 Comment

Nice! Based on your solution I tried the following: grouped = a.group_by {|e| e[:id_control_access]}; grouped.transform_values {|arr| arr.collect {|hash| hash.reject{|k,v| ![:input,:output].include?(k) } }.reduce(&:merge) }
0

The problem is that you are iterating over each hash and trying to access data in either the hash before or after. Take the first hash for example: {:id_control_access=>1, :input=>"Antena 1"}. You call antennas[:input] on it, so it displays "Antena 1". But then you call antennas[:output], and there is no output key in the current hash, so it's returning nil and causing the corresponding table cell to be blank.

You should consider updating the structure of your hashes, if you can, so that they look like {:id_control_access=>1, :input=>"Antena 1", :output=>"Antena 2"}. It seems to me to make more logical sense, and would solve the problem with your table.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.