在表关系问题中对信息进行分组和计数

时间:2013-10-29 20:50:21

标签: ruby-on-rails ruby ruby-on-rails-2

我做了一个应用程序来显示保险融资中的最后一次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

我该如何解决这个问题?

请有人帮我解决这个问题

感谢

2 个答案:

答案 0 :(得分: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的更聪明的方法缩短 lot ,你可以干掉显示逻辑,但我认为清晰度可能是一个好主意。

答案 1 :(得分: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 %>