Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1928886
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T07:02:55+00:00 2026-05-17T07:02:55+00:00

I have several Rails Active Record Models: ContactEmails, ContactCalls, ContactPostalcards. Each one represent a

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T07:02:58+00:00Added an answer on May 17, 2026 at 7:02 am

    If you have few companies, than @klew’s suggestion is probably the way to go. If you have many and only want to run one query, here’s a gist which illustrates using subqueries to get all of the counts: http://gist.github.com/602944

    It’s split into two examples, the first is really introductory to how the subqueries will work, the second removes all the redundancy so it’s easier to edit in the future.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several active record models in a rails app and I would like
I have several shell instances active while working on a Rails app (server, vim,
I'm using rails-rspec gem and I have several specs (models, controllers, etc). When I
I have a Rails app with several models with the same structure: class Item1
I have a database with several tables, each one containing columns that may not
I have a rails 3.2.2 app with patients who can have several types of
The main idea is that I have several worker instances of a Rails app,
I'm a rails beginner and have been several hours now trying to figure out
we have several tens of macro enabled excel files, each of those contains few
We have several offices around the US and one in India. Our IT department

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.