Topic: Eliminate unnecessary SQL Calls

I have a section of my website where I list all the topics available and the number of articles they have.  For example, the output is this:

Topic1 (5)
Topic2 (7)
Topic3 (4)

I could get this information is one shot using the following SQL Command:

select topic, count(*) from topics, posts where = posts.topic_id group by topic;

However, I don't know how to translate this into Rails code.  What I did was this in my Topic model:
  def self.topic_count
    @topic = Topic.find(:all)
    @stuff = { |topic|
      @posts = Post.find(:all, {:conditions => [ 'topic_id = ?',] })
      [ topic, @posts.length ]

And then used this in the view:
    <% Topic.topic_count.each do |topic, count| %>
      <li><%= link_to  "#{topic.topic} (#{count})", topic %></li>
    <% end %>

Does anyone see a better idea so I only need one SQL call instead of one for each topic like a am doing?  Thanks in advance.

Re: Eliminate unnecessary SQL Calls

For these cases, a Counter Cache Column is often used as a form of denormalization.

It saves the count of the children in a child_count column in the parent.
The advantage is that you don't even have to join the associated table anymore to get the count. But on the other hand, the parent gets updated whenever a child is added or removed ... it is not much of give and take perfomance_wise, and it's better than fiddling with some find_by_sql or similar

Re: Eliminate unnecessary SQL Calls

Thanks Duplex, that works really well (and was quite easy).  Those Railscasts are really great.