Pregunta

I did an app to show a the last net_insurance but when i try to count in Insurance Financing

Here my tables

|policies|
 |id|  |num_policy|
  1       12345
  2       54654

|insurances|
 |id|  |id_policy| |net_insurance| 
  1       1          1000
  2       2          2000
  3       2          3000
  4       1          5000     

|insurance_financing|       
  |id| |id_ensurance| |number|
   1         2           9888
   2         2           1444
   3         4           2444
   4         4           1445


 |trying to obtain|
   |num_policy|   |last_net_insurance|  |count_InsuranceFinancing_by_IdEnsurance|
       12345         3000                          2
       54654         5000                          2

This is my controller

class PolicyController < ApplicationController
    def generate_print
      @policies= Policy.find(:all)
    end
end

This is my model

class Policy < ActiveRecord::Base
  has_many :insurances
end

class Insurance < ActiveRecord::Base
  belongs_to :policy
  has_many :insurance_financing_details
end

class InsuranceFinancingDetail < ActiveRecord::Base
  belongs_to :insurance
end        

This is my view when i tried this

<% @policies.each do |p| %>

     <%= p.num_policy   %>
     <%  policy.insurances.last(1).each do |insurance| %>
        <% insurance.insurance_financing_details.each do |detail| %>
           <%= detail.number %>
        <% end %>
     <% end %>
<% end %>

I got this result

   |num_policy|   |last_net_insurance|  |count_InsuranceFinancing_by_IdEnsurance|
       12345         3000                          9888    1444
       54654         5000                          2444    1445

But i want

   |num_policy|   |last_net_insurance|  |count_InsuranceFinancing_by_IdEnsurance|
       12345         3000                          2
       54654         5000                          2

I tried this code and is not working

     <%  policy.insurances.last(1).each do |insurance| %>
        <% insurance.insurance_financing_details.size %>
     <% end %>

Tried this and is not working

     <%  policy.insurances.last(1).each do |insurance| %>
        <% insurance.insurance_financing_details.count %>
     <% end %>

And also tried but still not working

     <%  policy.insurances.last(1).insurance_financing_details.size %>

Please somebody can help me with this problem

I will really appreciate help

¿Fue útil?

Solución

You need to use <%= instead of <% when you want the result to be rendered:

<% insurance.insurance_financing_details.size %>

Should be:

<%= insurance.insurance_financing_details.size %>

Otherwise, it will render (in your case) the collection the block was iterating on.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top