views:

39

answers:

2

I have @contacts_added defined as follows:

@contacts_added = Contact.all(:conditions => ["date_entered >?", 5.days.ago.to_date])

Each contact belongs_to a Company.

I want to be able the count the number of distinct Companies that @contacts_added belong to. contacts_added will have many contacts that belong to a single company, accessible through a virtual attribute contacts_added.company_name

How do I do that?

+3  A: 
@contacts_added.map(&:company_name).uniq.length
Jakub Hampl
+2  A: 

sql (ORM) solution:

@contacts_added_companies = Contact.count(:joins => :company, :conditions => ["date_entered >?", 5.days.ago.to_date], :select => 'DISTINCT(company.id)' )
fl00r