I have several Rails Active Record Models: ContactEmails, ContactCalls, ContactPostalcards.
Each one represent a completed activity: a completed email, call, or postalcard to a specific Contact, and each Contact belongs to a Company.
I want a high-level summary by Company of all the Emails, Calls, and Postalcards within a specific date range:
def self.get_list(report_start_date, report_end_date)
self.find :all,
:select => "companies.name AS co_name,
companies.id AS comp_id,
COUNT(contact_emails.id) AS email_count,
COUNT(contact_calls.id) AS call_count,
COUNT(contact_letters.id) AS letter_count,
COUNT(contact_postalcards.id) AS postalcard_count",
:conditions => ['contact_emails.date_sent < ? and contact_emails.date_sent > ? or
contact_calls.date_sent < ? and contact_calls.date_sent > ?',
report_end_date, report_start_date, report_end_date, report_start_date],
:joins => [
"LEFT JOIN companies ON companies.id = contacts.company_id",
"LEFT JOIN contact_emails ON contact_emails.contact_id = contacts.id",
"LEFT JOIN contact_letters ON contact_letters.contact_id = contacts.id",
"LEFT JOIN contact_postalcards ON contact_postalcards.contact_id = contacts.id",
"LEFT JOIN contact_calls ON contact_calls.contact_id = contacts.id"
],
#:group => "companies.id"
:group => "companies.name"
end
This is how I output the matrix in my View:
<% @matrix_summary.each do |item| %>
<tr>
<td><%= link_to item.co_name, company_path(item.comp_id) %></td>
<td><%= item.email_count %> </td>
<td><%= item.postalcard_count %></td>
<td><%= item.call_count %></td>
<% end %>
</tr>
My expectation is each row should represent a Company, with the respective Email, Postalcard and Call counts for that one company (across all Contacts).
What I get is the same value (and a wrong value at that) for all Activities.
What am I doing that's not right?