Display fields from a hash in the view

1

I have a result of a variable that receives the output of an sql from postgres. I need to iterate in a view and I do not know how to display the given fields.

In products_controller I have:

 def show
    sql = "SELECT products.cod as cod_produto,products.descricao as desc_produto,colors.descricao as desc_cor,sizes.descricao as desc_tamanho,products.price as preco,variations.quantity as quantidade,variations.barcode as cod_barras FROM variations INNER JOIN products ON products.id = variations.product_id INNER JOIN sizes ON sizes.id = variations.size_id INNER JOIN colors ON colors.id = variations.color_id WHERE variations.product_id = 3 order by desc_tamanho, desc_cor asc"
    @results = ActiveRecord::Base.connection.exec_query(sql)


  end

The output of this command on the console is as follows:

SELECT products.cod as cod_produto,products.descricao as desc_produto,colors.descricao as desc_cor,sizes.descricao as desc_tamanho,products.price as preco,variations.quantity as quantidade,variations.barcode as cod_barras FROM variations INNER JOIN products ON products.id = variations.product_id INNER JOIN sizes ON sizes.id = variations.size_id INNER JOIN colors ON colors.id = variations.color_id WHERE variations.product_id = 3 order by desc_tamanho, desc_cor asc

=> #<ActiveRecord::Result:0x0000010bb60b28 @columns=["cod_produto", "desc_produto", "desc_cor", "desc_tamanho", "preco", "quantidade", "cod_barras"], @rows=[["CB002", "Cueca Boxer Infantil Cotton", "Azul", "G", "10.9", "-2", "CB002M100001090"], ["CB002", "Cueca Boxer Infantil Cotton", "Branco", "G", "10.9", "-2", "CB002M200001090"], ["CB002", "Cueca Boxer Infantil Cotton", "Preto", "G", "10.9", "-2", "CB002M300001090"], ["CB002", "Cueca Boxer Infantil Cotton", "Vermelho", "G", "10.9", "-4", "CB002M500001090"]], @hash_rows=nil, @column_types={"cod_produto"=>#<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::OID::Identity:0x00000102b00178>, "desc_produto"=>#<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::OID::Identity:0x00000102b00178>, "desc_cor"=>#<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::OID::Identity:0x00000102b00178>, "desc_tamanho"=>#<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::OID::Identity:0x00000102b00178>, "preco"=>#<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::OID::Decimal:0x00000102b001f0>, "quantidade"=>#<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::OID::Integer:0x00000102b00c40>, "cod_barras"=>#<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::OID::Identity:0x00000102b00178>}>

What is the best way to go through this hash and display the output of each field in my view and its values as we do with an object? (Ex @ results.cod_product)

    
asked by anonymous 06.08.2016 / 21:48

3 answers

0

Try this in your view (considering ERB):

<% @results.each do |result| %>
  <%= result.cod_produto %> <br>
  <%= result.desc_produto %>
  <%= result.desc_cor %>
  ...
<% end %>
    
07.08.2016 / 03:03
0

You can iterate over @results :

<% @results.each do |result|  %>
  <b>Produto:</b> <%= result['desc_produto']  %> <br/>
  <b>Cor:</b> <%= result['desc_cor']  %> <br/>
   (...)
<% end %>

Or even iterate over every hash :

<% @results.each do |result|  %>
  <% result.each_pair do |label, value|  %>
    <b><%= label %>:</b> <%= value  %> <br/>
  <% end  %>
<% end %>

Or a more presentable form, in table:

<table border="1">
  <thead>
    <tr>
      <th>Produto</th>
      <th>Cor</th>
      <th>(...)</th>
    </tr>
  </thead>

  <tbody>
    <% @results.each do |result|  %>
    <tr>
      <td><%= result['desc_producto'] %></td>
      <td><%= result['desc_cor']  %></td>
      <td>(..)</td>
    </tr>
    <% end  %>
  </tbody>

</tabble>

Result:

    
08.08.2016 / 14:22
0
sql = "SELECT products.cod as cod_produto,products.descricao as desc_produto,colors.descricao as desc_cor,sizes.descricao as desc_tamanho,products.price as preco,variations.quantity as quantidade,variations.barcode as cod_barras FROM variations INNER JOIN products ON products.id = variations.product_id INNER JOIN sizes ON sizes.id = variations.size_id INNER JOIN colors ON colors.id = variations.color_id WHERE variations.product_id = 3 order by desc_tamanho, desc_cor asc"

# a maneira que você vai ordenar o seu array na view vai depender do teu query select. 
# @rows=[["CB002", "Cueca Boxer Infantil Cotton", "Azul", "G", "10.9", "-2", "CB002M100001090"]
# acima mostra como o primeiro elemento do array e como ele está ordenado

#...
<tbody>
  <% @results.rows.each do |rows|  %>
    <tr>
      <% rows.each do |result|  %>
        <td><%= result %></td>
      <% end  %>
    </tr>
  <% end  %>
</tbody>
    
14.08.2016 / 17:07