Counting and grouping at the same time

后端 未结 4 1984
轻奢々
轻奢々 2020-12-08 01:39

I have a Mail model with the following schema:

t.string   \"mail\"
t.integer  \"country\"
t.boolean  \"validated\"
t.datetime \"created_at\"
t.d         


        
4条回答
  •  醉梦人生
    2020-12-08 02:03

    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.

提交回复
热议问题