How do I do a count on that meet a specific condition, dependent on several has_many relationships i
        Posted  
        
            by Angela
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Angela
        
        
        
        Published on 2010-06-09T04:57:00Z
        Indexed on 
            2010/06/09
            5:02 UTC
        
        
        Read the original article
        Hit count: 265
        
I have a Model Campaign.
A Campaign has many Events.
Each Event has an attribute :days.
A Campaign also has_many Contacts.
Each Contact as a :date_entered attribute.
The from_today(contact,event) method returns a number, which is the number of days from the contact's :date_entered till today minus the event's :days. In other words, a positive number shows the number of days from today till the :days of the event is elapsed. If it is negative, if means that the number of days that has elapsed since the :date_entered is greater than the :days attribute of an event. In other words, the event is overdue.
What I would like to be able to do is do campaign.overdue and this would result in a total number of contacts that have an overdue event. It shouldn't count multiple events for a single contact, just one contact.
How do I do that? It seems like I would need to cycle through all the events for every contact and keep a counter but I'm assuming that there is a better way.
© Stack Overflow or respective owner