Counting all association objects and updating a counter
<p>I have 2 models:</p>

<pre><code>class Products &lt; ActiveRecord::Base
  has_many :purchases

class Purchase &lt; ActiveRecord::Base
  belongs_to :Product
</code></pre>

<p>The products model has a column <code>purchase_count</code></p>

<p>I want a very fast efficient way to update this counter that basically runs this sql:</p>

<pre><code>UPDATE products SET purchase_count = (SELECT COUNT(*) from purchases where active=1 and product_id = 123) where id = 123
</code></pre>

<p>Can I do this with ActiveRecord ?  If not, how can I do this with just raw sql?</p>