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?
@contacts_added.map(&:company_name).uniq.length
sql(ORM)解决方案:
@contacts_added_companies = Contact.count(:joins => :company, :conditions => ["date_entered >?", 5.days.ago.to_date], :select => 'DISTINCT(company.id)' )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.