Compare "id" of two related objects

0

Maybe the question is simple for you.

I use the Bidon and Buy models, which are related, when I show the detail of the drum suppose with ID: 1, in the I am showing the last purchases that are made with that drum, what I can not get is that when I select a different drum, it shows me the purchases made with that drum and not the last ones only.

this is the (views / bidons / show.html.erb)

<% @compras.each do |compra| %>
<ul>
<li>
<small>Cantidad utilizada:</small> <span class="badge badge-inverse">
<small><%= compra.cantidad_comprada %></small></span> | <small>Producto:</small> 
<span class="badge badge-purple"><small><%= compra.product %></small></span>
<%= link_to '<i class="fa fa-eye">
</i>
<span class="sr-only">Detalles</span>'.html_safe, compra_path(compra), :title => "Mostrar detalles", :class => "green bigger-140 show-details-btn" %>
</li>
</ul>
<% end %>

How can I show only the purchases made with a specific drum?

Try to buy the corresponding ids: @ bidon.id and @ compra.bidon_id, if these two are equal, the result would be true in the show.html.erb, but I think I'm missing something.

I appreciate your time and understanding.

Mosiah.

    
asked by Mosiah Ricardo 26.02.2017 в 08:33
source

1 answer

1

If I understand correctly, your problem is that @compras has all the purchases, and not just the purchases of your drum. Assuming:

  • That your view has @bidon assigned to the relevant drum
  • Buy belongs_to bidon
  • Bidon has_many purchases

You should be able to do:

<% @bidon.compras.each do |compra| %>

Another possibility would be to change @compras on your controller by:

@compras = @bidon.compras
    
answered by 26.02.2017 / 14:50
source