Reputation: 1149
I did an app to show a column from another table but i want to know how to count the results instead of showing information
Here my tables
|policies|
|id| |num_policy|
1 11111
2 22222
|insurances|
|id| |id_policy| |net_insurance|
1 1 33333
2 1 44444
3 2 55555
4 2 66666
|insurance_financing|
|id| |id_ensurance| |number|
1 1 1544
2 2 858
3 3 8484
4 4 848
I'm trying to do
|num_policy| |count|
11111 2
22222 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
<% @policies.each do |p| %>
<%= p.num_policy %>
<% p.insurances.each do |insurance| %>
<% insurance.insurance_financing_details.each do |detail| %>
<%= detail.number %>
<% end %>
<% end %>
<% end %>
Please somebody can help me with this problem
I will really appreciate help
Upvotes: 0
Views: 48
Reputation: 5249
Policies.joins(:insurances).group("policies.id").count
will give you a hash where the key is the policies.id and the value is the count of the insurances for each policy.
NOTE: if a policy does not have any insurances, it will not be included in the result.
Upvotes: 1