Skip to content

Instantly share code, notes, and snippets.

@losvedir
Created December 3, 2012 20:29
Show Gist options
  • Select an option

  • Save losvedir/4197751 to your computer and use it in GitHub Desktop.

Select an option

Save losvedir/4197751 to your computer and use it in GitHub Desktop.
Rails 3 ActiveRelation counting, grouping, sorting, and limiting
Model.joins(:relation)
.merge(Relation.scope)
.count(group: 'column or postgres function',
order: 'count_all DESC',
limit: 10
)
# Note: the count_all is the 'AS' variable ActiveRelation makes for you.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment