title: How do I group by and order by count in Rails through a join table?
body: <p>How do I translate this SQL into Rails?</p>
  <pre><code>select tags.tag, count(*) c 
    from taggings, tags 
    where taggings.post_id = 1 and taggings.tag_id = tags.id 
    group by tag 
    order by c desc;
  </code></pre>
  <p>I tried to use</p>
  <blockquote>
  <p>Post.first.tags.select(:tag, &quot;count(*) as count&quot;).group(:tag).order(count: :desc)</p>
  </blockquote>
  <p>but it gave an error</p>
  <blockquote>
  <p>ArgumentError: wrong number of arguments (2 for 0..1)</p>
  </blockquote>
  <h3>Models</h3>
  <pre><code>class Post &lt; ActiveRecord::Base
    has_many :taggings, :dependent =&gt; :destroy
    has_many :tags, :through =&gt; :taggings
  
  
  class Tagging &lt; ActiveRecord::Base
    belongs_to :post
    belongs_to :user
    belongs_to :tag
  </code></pre>