Skip to content

Instantly share code, notes, and snippets.

@Hates
Created June 10, 2011 11:05
Show Gist options
  • Save Hates/1018625 to your computer and use it in GitHub Desktop.
Save Hates/1018625 to your computer and use it in GitHub Desktop.
scope :ordered_by_article_count,
select("users.*, c.count as article_count").
joins("LEFT JOIN (SELECT user_id, COUNT(user_id) as count
FROM articles WHERE state = 'active' GROUP BY user_id) as c ON c.user_id = users.id").
where("c.count IS NOT NULL").
order("c.count DESC")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment