I just created a counter_cache field and the controller looks like this.
@users = User.where(:sex => 2).order(\'received_likes_count\')
This basically does the same thing:
after_save :cache_post_count_on_tags def cache_post_count_on_tags tags.each {|t| tag.update_attribute(:posts_count, t.posts.size)} end
And you need a posts_count column on tags, or whatever associations you have.
posts_count