Rails 3 Choose a separate order by the number of seats

In one of my models, I have a country column. How can I choose the best 3 countries based on how many models this country has?

+3
source share
1 answer

Without additional information, you can try:

YourModel.group('country').order('count_country DESC').limit(3).count('country')

when you call the counter on field rails, it automatically adds a field to your request AS count_field_name.

The graph should be called at the end of the request because it returns an ordered hash.

+7
source

All Articles