JP Silvashy
JP Silvashy

Reputation: 48525

How to sort results by count and return in order of most to least using active record in Rails

I have a searches table, which has all the searches that get run on our site. I want to pull up the most popular searches. Like say there are 130 records with the column of phrase being "cheese", how do I sort the results by count and return them in order of most to least using active record?

Upvotes: 0

Views: 323

Answers (1)

jyoseph
jyoseph

Reputation: 5455

Taken from the example I linked in the comments above.

Searches.find(:all, :select => '*, count(*) AS count, phrase', :group => 'phrase', :order => 'count DESC')

Although I just tried this on my own sqlite db and it worked fine (rails 3)

Searches.count(:all, :group => 'phrase', :order => 'count(*) DESC')

Upvotes: 2

Related Questions