title: How can I find an associated table oldest record filtering by one of it's attributes?
body: <p>I have the model <code>Subscription</code> which <code>has_many</code> <code>Version</code>s.</p>
  <p>A <code>Version</code> has a <code>status</code>, <code>plan_id</code> and <code>authorized_at</code> date.</p>
  <p>Any changes made to a <code>Subscription</code> comes from a <code>Version</code> modifications updating it's parent <code>Subscription</code>.</p>
  <p>The goal is to find each Subscription's <code>Version</code> with the oldest <code>authorized_at</code> date <code>WHERE</code> the <code>versions.plan_id</code> is the same as the <code>subscriptions.plan_id</code> (in other words I need the authorization date of the <code>Version</code> where the <code>plan_id</code> changed to the current <code>Subscription</code>'s <code>plan_id</code>).</p>
  <p>This is the query I've come up with. I'm getting an error in the aggregate function syntax:</p>
  <pre><code>syntax error at or near &quot;MIN&quot; LINE 3: MIN (authorized_at) from versions ^
  </code></pre>
  <p>query:</p>
  <pre><code>select subscriptions.id,
  MIN (authorized_at) from versions
  where versions.plan_id = subscriptions.plan_id
  ) as current_version
  from subscriptions
  join versions on subscriptions.id = versions.subscription_id
  where versions.status = 'processed'
  </code></pre>
  <p>I also am not sure if I should be grouping the versions by <code>plan_id</code> and then picking from each group. I'm kind of lost.</p>