samedi 27 juin 2015

mapping values through deeply nested associations

A controller extracts data from deeply nested associations where bilancino belongs_to :operativo which in turn belongs_to :cdg as such:

@cdgs = Cdg.order("id ASC").all
@bilancinos = Bilancino.joins(:operativo).order('cdg_id ASC').all

(with a where clause in there).

However when rendering

<% @cdgs.each do |cdg| %>
  <% @cdgs_for_bilancino = @bilancinos.select{ |i| i.operativo.cdg_id == cdg } %>
  <%= @cdgs_for_bilancino.each do |bilancino| %> XX <% end %>
  <%= number_with_precision(@cdgs_for_bilancino.map(&:saldo).sum, :precision => 0, :delimiter => "\u00a0")  %>
<% end %>

is generating an empty array, yet if underneath the following

<% @bilancinos.each do |bilancino| %>
  <%= bilancino.operativo.cdg.conto %> <%= bilancino.saldo %><br />
<% end %>

will render. Thus the expression @bilancinos.select{ |i| i.operativo.cdg_id is missing the nested target somehow.

What is the proper syntax?

Aucun commentaire:

Enregistrer un commentaire