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)