15

I have a Mail model with the following schema:

t.string   "mail"
t.integer  "country"
t.boolean  "validated"
t.datetime "created_at"
t.datetime "updated_at"

And I want to find the top 5 countries in the database, so i go ahead and type

@top5 = Mail.find(:all,:group =>  'country',:conditions => [ "validated = ?" , "t" ], :limit => 5 )

This will tell me the groups(i need an order by i dont know how to write)

@top5 = Mail.count(:all,:group =>  'country',:conditions => [ "validated = ?" , "t" ], :limit => 5 )

This will tell me how many mails are in each group

Im wondering if i can group and count in just one go

4 Answers 4

24

Try:

Mail.count(:group => 'country', :conditions => ['validated = ?', 't'])

I'm not sure count accepts :limit though.

EDIT:

I think this is more readable:

Mail.count(:group => :country, :conditions => {:validated => true})

1
  • In order to use the limit option you have to do: Mail.limit(10).order("count_all desc").count(group: :country). Similar to what tee suggests. For specifying conditions I suggest using a scope on the model, or the where option.
    – Hendrik
    Commented Dec 23, 2012 at 17:59
22

With Rails 3 you can simplify it further:

Mail.where(validated: true).count(group: :country)

You can order by fields in the group - in this case only :country would be valid:

Mail.where(validated: true)
    .order(:country)
    .count(group: :country)

You can also order by the count, using "count_all":

Mail.where(validated: true)
    .order("count_all desc")
    .count(group: :country)

You can also limit the number of groups returned. To do this you must call limit before calling count (because #count returns ActiveSupport::OrderedHash):

Mail.where(validated: true)
    .order("count_all desc")
    .limit(5)
    .count(group: :country)

Updated syntax for Rails 4:

Mail.where(validated: true)
    .group(:country)
    .count
19
Mail.find(
    :all, 
    :select => 'count(*) count, country', 
    :group => 'country', 
    :conditions => ['validated = ?', 't' ], 
    :order => 'count DESC',
    :limit => 5)

This should give you records that have a country attribute and a count attribute.

3
  • true should be used instead of 't' Commented Dec 20, 2010 at 19:04
  • 1
    @airportyh Rails API says that ActiveRecord.find is deprecated (apidock.com/rails/ActiveRecord/Base/find/class). Do you know the best way to do this on Rails 3?
    – dcarneiro
    Commented Jan 30, 2012 at 18:06
  • 1
    @Daniel: I've thought that before but look closely: it says "moved or deprecated" and actually it has been moved to ActiveRecord::FinderMethods#find. (I believe you are still supposed to call Mail.find to access this method in its new location.)
    – antinome
    Commented Aug 22, 2012 at 16:18
0

I too had to group data with city name and showing count of how many rows are there for each city with specific condition. So this is how I did:

CityData.where(:status => 1).group(:city_name).count

Output of this was:

{:Mumbai => 10, :Dublin => 7, :SF => 9}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.