I have a members and a projects table. They are associated via a has_many
:through. I want to find out how many members are interested in a specific
project. All I could come up with was:
a = Project.find(:first, :include => :members, :conditions =>
"projects.name='my project'").members.size
Actually, the problem is more complex than this simple example. I want to
find count of members interested in a specific project grouped by date and
filtered by a specific date range.
Is there a calculations magical way to do this or is it strictly
find_by_sql?
@signups = Member.find_by_sql(
["select DATE(created_on) as created_on, count(projects.id)
as counter from members
join assignments on members.id = assignments.member_id
join projects on projects.id = assignments.project_id
where projects.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.