You can do this with a complicated active record query, however, the easiest thing by far is to just add a :counter_cache
on association. You just add a field on your Post model called comments_count
and then in your Comment model:
belongs_to :post, counter_cache: true
Then since it's just a column like all your other columns you can order by it.