HMT: Count Associated Items

I have a members and a projects table. They are associated via a
:through. I want to find out how many members are interested in a
project. All I could come up with was:

a = Project.find(:first, :include => :members, :conditions =>
“‘my project’”).members.size

Actually, the problem is more complex than this simple example. I want
find count of members interested in a specific project grouped by date
filtered by a specific date range.

Is there a calculations magical way to do this or is it strictly



View this message in context:
Sent from the RubyOnRails Users mailing list archive at

Check out :counter_cache at

It’s also explained on the migrations screencast.

Hope that helps.

I wound up using find_by_sql:

 @signups = Member.find_by_sql(
     ["select DATE(created_on) as created_on, count(

as counter from members
join assignments on = assignments.member_id
join projects on = assignments.project_id
group by week(members.created_on)", project_id]

Which gives me a weekly summary of member creation records aggregated
(counted) by week for a certain project. I’m afraid a counter cache
would not be specific enough for the problem I’m trying to solve. I
was hoping there was some natural Rails syntax in Calculations that
would get me close to this.