Retrieve field with join in view in RUBY / RoR

1

I have a big question, I am a beginner in Ruby, and I am not able to print the value of a field in my view, the result is that.

My Model:

    class Pedido < ActiveRecord::Base
      has_many :produtos      

      scope :waiting, -> { where(status: 1) }
end

My Controller:

class Backoffice::PedidosController < BackofficeController

  def index
    @pedidos = Pedido.waiting       
  end

end

And in my View it looks like this:

<tbody>               
  <% @pedidos.each do |pedido| %>
    <tr> 
      <th><%=pedido.id%></th>
      <th><%=pedido.status%></th>
      <th><%=pedido.created_at%></th>
      <th><%=pedido.produtos.select(:valor) %></th>
     </tr>
   <% end %>
 </tbody>

I've tried it in several ways and it always prints what's in print, if I change the line to

  

<% = order.products.product% >

The error on the page.

Can someone give me a light?

    
asked by anonymous 23.08.2017 / 19:47

1 answer

0

Try specifying the Produto of the association, for example:

<%= pedido.produtos.first.valor %> In this case, it returns the first Produto of the association.

I do not know how your implementation will be, but since Pedido has many Produtos , when you search for that association, it returns a collection of Produtos , and you need to specify which one you want.

Or, if you want, make a sum of the value of Produtos :

<%= pedido.produtos.sum(:valor) %>

    
23.08.2017 / 20:35