title: rails activerecord - distinct counter cache implementation
body: <p>I have these tables:</p>
  
  <p><code>broadcast
  id
  name
  </code></p>
  
  <p><code>email
  id
  broadcast_id
  user_id
  subject
  </code></p>
  
  <p><code>email_open
  id
  email_id
  user_id
  </code></p>
  
  <p>I want to keep a count of the email_open records in my <code>broadcast</code> table. </p>
  
  <p>Is the most efficient way of doing this by having a <code>broadcast_id</code> in my <code>email_open</code> table? If it is.. then I know I can just do this in my <code>email_open</code> model:</p>
  
  <p><code>belongs_to :broadcast, counter_cache: =&gt; true</code></p>
  
  <p>Then, I add a <code>email_open_count</code> to my broadcast table... but I'm wondering if there's a way to do it without doing this.</p>
  
  <p>Also, multiple users can have repeated records in <code>email_open</code>.. how do I make the count be of distinct <code>user_id</code>?</p>
  
  <p>For example, <code>user_id</code> 1 can open an email 5 times but I just want the <code>email_open_count</code> to be 1.</p>
  
  <p>Thanks</p>