我做了一个应用程序来显示保险融资中的最后一个 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
我该如何解决这个问题???
请有人可以帮我解决这个问题
谢谢