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? Thanks, steve -- View this message in context: http://www.nabble.com/HMT%3A-Count-Associated-Items-tf2786928.html#a7775495 Sent from the RubyOnRails Users mailing list archive at Nabble.com. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Check out :counter_cache at http://www.rubyonrails.org/api/classes/ActiveRecord/Associations/ClassMethods.html It''s also explained on the migrations screencast. http://media.rubyonrails.org/video/migrations.mov Hope that helps. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I wound up using 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. Steve On Dec 10, 2006, at 4:43 AM, Chris.Mohr wrote:> > Check out :counter_cache at > http://www.rubyonrails.org/api/classes/ActiveRecord/Associations/ > ClassMethods.html > > It''s also explained on the migrations screencast. > http://media.rubyonrails.org/video/migrations.mov > > Hope that helps. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---