Grouping and counting information in table relatio

2019-09-14 15:14发布

问题:

I did an app to show a the last net_insurance and count in Insurance Financing but i'm trying to group and count when is expired and not expired

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|  |expiration_date|
   1         2           9888      26/10/2013
   2         2           1444      27/10/2013
   3         4           2444      28/10/2013
   4         4           1445      01/11/2013 


 |trying to obtain|
   |num_policy|   |last_net_insurance|  |count_InsuranceFinancing_by_IdEnsurance|
       12345         3000                               2 expired
       54654         5000                          1 expired, 1 not expired

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| %>
             <% if Date.today > detail.expiration_date %>
                 <%= "EXPIRED" %>
             <% else %>  
                 <%= "NOT EXPIRED" %>
             <% end %>
          <% end %>
     <% end %>
<% end %>

I got this result

   |num_policy|   |last_net_insurance|  |count_InsuranceFinancing_by_IdEnsurance|
       12345         3000                          EXPIRED    EXPIRED
       54654         5000                          EXPIRED    NOT EXPIRED

But i want

   |num_policy|   |last_net_insurance|  |count_InsuranceFinancing_by_IdEnsurance|
       12345         3000                          2 expired
       54654         5000                          1 expired, 1 not expired

I tried this but is counting all and need to group by expiration

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

   |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.each do |detail| %>
                <% if Date.today > detail.expiration_date %>
                  <%= "YES".size %>
                <% else %>  
                  <%= "NOT".size %>
                <% end %>
              <% end %>
            <% end %>

And also tried

            <%  policy.insurances.last(1).each do |insurance| %>
              <% insurance.insurance_financing_details.each do |detail| %>
                <% if  Date.today > detail.expiration_date %>
                  <%= "Yes".count  %>
                <% else %>  
                  <%=  "NO".count  %>
                <% end %>
              <% end %>
            <% end %>

But i'm getting

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

How can i fix this problem???

Please somebody can help me with this problem

THanks

回答1:

You'll have to rethink this a bit; you're confusing display logic and a calculation that you want to perform. Some things first--

 <%= "YES".size  %>

This will always be 3, for the same reason that "NO".size will always be 2 and "THISHAS19CHARACTERS".size will always be 19.

Anyway, you need to untangle your display logic from the calculation. Count the expired and unexpired policies first, then display them. Here's a really simple example:

<% @policies.each do |p| %>
     <%= p.num_policy   %>
     <% 
        expired = 0
        not_expired = 0 

        policy.insurances.last(1).each do |insurance| 
          insurance.insurance_financing_details.each do |detail|
            if Date.today > detail.expiration_date
              expired += 1
            else
              not_expired += 1 
            end
          end
        end
     %>
     <% if not_expired > 0 %>
       <%= expired %> expired, <%= not_expired %> not expired
     <% else %>
       <%= expired %> expired
     <% end %>
<% end %>

You can make that a lot shorter using cleverer methods from Enumerable and you can DRY up the display logic, but I figured clarity was probably a good idea.



回答2:

This can be done much more simply using the group_by method:

        <%  policy.insurances.last(1).each do |insurance| %>
          <% groups = insurance.insurance_financing_details.group_by {|detail| Date.today > detail.expiration_date } %>
          <%= "#{groups[true].size} expired" if groups[true] %>
          <%= ", " if groups.size == 2 %>
          <%= "#{groups[false].size} not expired" if groups[false] %>
          <% end %>
        <% end %>