0

我做了一个应用程序来显示保险融资中的最后一个 net_insurance 和计数,但我正在尝试分组和计数何时过期和未过期

这是我的桌子

|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

这是我的控制器

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

这是我的模型

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        

这是我尝试这个时的看法

<% @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 %>

我得到了这个结果

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

但我想要

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

我试过了,但正在计数,需要按到期时间分组

      <%  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

我尝试了此代码但无法正常工作

            <%  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 %>

也试过

            <%  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 %>

但我得到

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

我该如何解决这个问题???

请有人可以帮我解决这个问题

谢谢

4

2 回答 2

1

您将不得不重新考虑一下;您混淆了显示逻辑和要执行的计算。先说一些事情——

 <%= "YES".size  %>

这将永远是 3,出于同样的原因,"NO".size永远是 2 并且"THISHAS19CHARACTERS".size永远是 19。

无论如何,您需要从计算中解开显示逻辑。先统计过期和未过期的保单,再显示。这是一个非常简单的例子:

<% @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 %>

您可以使用更聪明的方法将其缩短很多Enumerable,并且您可以干燥显示逻辑,但我认为清晰可能是一个好主意。

于 2013-10-29T22:45:05.523 回答
0

这可以使用以下方法更简单地完成group_by

        <%  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 %>
于 2013-10-30T02:28:32.533 回答