Display fields from a hash in the view

Asked

Viewed 295 times

1

I have a result of a variable that receives the output of an sql from postgres. I need to iterate it in a view and I don’t know how to display the specified 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 respective values as we do with an object ? (Ex @Product Name)

3 answers

0

You can iterate on @results:

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

Or even make an iteration on each hash:

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

Or even a more presentable shape, 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>

Upshot:

inserir a descrição da imagem aqui

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>

0

Try as follows in your view (considering ERB):

<% @results.each do |result| %>
  <%= result.cod_produto %> <br>
  <%= result.desc_produto %>
  <%= result.desc_cor %>
  ...
<% end %>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.