Ruby-on-rails – rails 3 Select different sequences by the number of occurrences

In one of my models, I have a country column. How would I choose the top 3 countries/regions based on the number of models in that country/region?
without any further information, you can try this:

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

When you call count on the field, rails The AS count_field_name field is automatically added to the query.

Count must be called at the end of the query because it returns an ordered hash.

In my In one of the models, I have a country column. How would I choose the top 3 countries/regions based on the number of models in that country/region?

Without any further information, you can try this:

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

When you call count on the field, rails will automatically add the AS count_field_name field to the query. p>

Count must be called at the end of the query because it returns an ordered hash.

Leave a Comment

Your email address will not be published.