Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I extract a "table" from an array of ActiveRecords in Rails?
    text
    copied!<p>I have several similar Models (ContactEmail, ContactLetter, ContactPostalcard).</p> <p>Each instance (record) in, say, ContactEmail, means a specific Email template was sent to a specific Contact.</p> <p>So, each one (e.g. ContactEmail) <code>belongs_to :contact</code></p> <p>So, in ContactEmail model, there is an attribute <code>ContactEmail.contact_id</code>.</p> <p>Each Contact has a virtual attribute company_name.</p> <p>So, when I write the following, I will get all emails sent within a specific set a conditions (in this case, time):</p> <pre><code>@sent_emails = ContactEmail.find(:all, :conditions =&gt; "conditions here") </code></pre> <p>So, <code>@sent_emails.size</code> would tell me the total of all emails sent.</p> <p><strong>My challenge: how do I extract more granularity, by unique company across the different models?</strong></p> <p>The output I want would look like the following:</p> <pre><code>FROM 8/1/10 TO 8/10/10 (where the dates are dynamic) Calls Letter Postalcards Company 1 4 2 4 Company 2 10 4 6 Company 3 2 3 4 </code></pre> <p>So Company3 has 2 Calls, which means there were two records of ContactCalls where the sent_date fell between the two dates, and where the associated contact belongs to Company 3.</p> <p>Company 1-3 is not set ahead of time. It needs to be extracted from the pool of ContactCalls, ContactLetters, and ContactPostalcards.....</p> <p>The challenge is that I don't know what the companies are. They are attributes of the contacts part of each distinct record. So in some cases, I may have Company2 has 0 letters.</p> <p>Thanks for any guidance! :)</p> <p><strong>How I can find a company on a given record for ContactEmail model:</strong></p> <pre><code>ContactEmail.contact.company.name </code></pre> <p>This will return the associated company for a specific ContactEmail</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload